【发布时间】:2017-09-20 15:42:07
【问题描述】:
我正在使用带有 nodejs express 框架的猫鼬
这是 mongo 中的文档:
{
"_id" : ObjectId("59c1fe16b68d844d8473c6fe"),
"is_active" : "0",
"membership_expiry_date" : ISODate("2018-09-20T05:35:18.485Z"),
"date_of_joining" : ISODate("2017-09-20T05:35:18.485Z"),
"user_name" : "Max M",
"first_name" : "Max",
"password" : "MyPwd",
"address" : "NewTown",
"email_id" : "max@gmail.com",
"phone_number" : "12345678901",
"books_borrowed" : [
{
"due_date" : ISODate("2017-09-22T05:35:18.511Z"),
"book_id" : "1234",
"is_renewed" : true,
"_id" : ObjectId("59c1fe16b68d844d8473c700")
},
{
"due_date" : ISODate("2017-09-22T05:35:18.531Z"),
"book_id" : "1235",
"is_renewed" : false,
"_id" : ObjectId("59c1fe16b68d844d8473c6ff")
}
],
"__v" : 0
}
现在当我尝试访问 book_borrowed where user_name=Max and book_id=1234 我正在使用
model.findOne({"user_name":"Max M","book_id":1234}, {
'books_borrowed': 1
},
function(err, res) {
console.log("##Err:: " + JSON.stringify(err) + " response:: " + JSON.stringify(res));
if (err) {
return callback('Error while updating...');
} else {
return callback(null, res);
}
});
这会返回 error = null 并且 response 也是 null。
如果我将 book_id 替换为“books_borrowed.book_id”,那么我会得到 p>
{
"_id": "59c1fe16b68d844d8473c6fe",
"books_borrowed": [
{
"due_date": "2017-09-22T05:35:18.511Z",
"book_id": "1234",
"is_renewed": true,
"_id": "59c1fe16b68d844d8473c700"
},
{
"due_date": "2017-09-22T05:35:18.531Z",
"book_id": "1235",
"is_renewed": false,
"_id": "59c1fe16b68d844d8473c6ff"
}
]
}
其中还包含 book_id = 1235 的数据。
我只想要包含 book_id=1234 数据的对象。
我在这里缺少什么?此外,我不希望将这些对象 ID 插入到我的 books_borrowed 对象中。
【问题讨论】:
标签: node.js mongodb express mongoose node-mongodb-native