【发布时间】:2018-10-26 18:45:50
【问题描述】:
首先为我的英语不好找借口,但我会尽力解释自己。我是一个试图用 express 开发 nodejs 项目的学生,直到现在我在单个 json 文件中用作数据库并通过它工作。但现在我想迁移到 Mongodb。我已经用 "mongoimport --db RestauranteSin" --collection "Restaurante" --file 'filename'" 导入了我的数据库,所以它导入了。
我接下来要做的是创建一个新端点
app.get('/mongoAllRestaurants', (req, res) => {
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect("mongodb://localhost:27017/", { useNewUrlParser: true },(err, db) => {
if (err) throw err;
var dbo = db.db("RestauranteSin");
var ObjectId = require('mongodb').ObjectID;
dbo.collection("Restaurante").find({_id:ObjectId("5bd218627c5b747cdb14c51e"), restaurantes: {$elemMatch : {titulo_restaurante: "BarJanny"}}}).toArray((err, result) => {
if (err) throw err;
console.log(result[0]);
res.send(result);
db.close();
});
});
});
我的数据库是这样的:
[
"_id" : "345678987654",
"restaurantes": [
{
"titulo_restaurante": "example1",
...
...
...
},
{
"titulo_restaurante": "example2",
...
...
...
},
...
...
...
]
]
这就是问题所在。 ¿为什么如果我进行查询,它会返回我所有的数据库而没有过滤器?我有很多查询组合,它总是返回给我所有的数据库或空数组?结果我需要这样的东西:
{
"titulo_restaurante": "example1",
...
...
...
}
【问题讨论】: