javascript - Nodejs MongoDb query returns everything instead of matching document
问题描述
Json Structure:
{"address": {"building": "1007", "coord": [-73.856077, 40.848447], "street": "Morris Park Ave", "zipcode": "10462"}, "borough": "Bronx", "cuisine": "Bakery", "grades": [{"date": {"$date": 1393804800000}, "grade": "A", "score": 2}, {"date": {"$date": 1378857600000}, "grade": "A", "score": 6}, {"date": {"$date": 1358985600000}, "grade": "A", "score": 10}, {"date": {"$date": 1322006400000}, "grade": "A", "score": 9}, {"date": {"$date": 1299715200000}, "grade": "B", "score": 14}], "name": "Morris Park Bake Shop", "restaurant_id": "30075445"},...
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'restaurant';
MongoClient.connect(url, function(err, client) {
const db = client.db(dbName);
const collection = db.collection('restaurants');
collection.find({}, {"restaurant_id": "40386837"}).toArray(function(err,docs){
console.log(docs)
});
});
Output:
There is only one record that matches the criteria "restaurant_id": "40386837" but I am getting all of it.
解决方案
我认为您的查询应该是您通过的第一件事,而不是第二件事:
find(query, projection)
所以:
collection.find({"restaurant_id": "40386837"})
请参阅文档:https ://docs.mongodb.com/realm/mongodb/actions/collection.find/
推荐阅读
- python - 在 Python 中提取文件的创建时间
- javascript - 项目的 vue.draggable 和持久位置
- azure-data-factory - 如何从 azure 数据工厂中的 Web 活动输出中获取特定字段
- javascript - 未处理的拒绝(TypeError):无法读取未定义 MERN 的属性“长度”
- c - 如何理解 `int (*pixels)[image->size_x];`?
- javascript - ES6 字符串到 ReadableStream
- javascript - 在集合中的所有文档中查找给定 json 属性的不同值
- java - 在 Spring hibernate 中使用 order by、limit 删除记录抛出 InvalidDataAccessResourceUsageException
- sql - 如何从 SQL 中的所有整数数组中获取特定数字
- html - 如何使用 Python 进行网页抓取来下载多个图像?到目前为止,我只设法使其与一张图片一起使用