[英]Mongodb find method always return all elements of the documents
我有student
收藏
{
_id: 1,
name: "Student",
tasks: [{ month: 1, year: 2018 }]
}
我需要一個僅返回任務數組的查詢:
db.collection('students').find(
{
'tasks.month': 1,
'tasks.year': 2018
},
{
'tasks.$' : 1
}
).toArray((err, res) => {
console.log(res)
})
這將返回所有帶有所有字段的文檔,包括name
等等。
find
將返回一個cursor
。 根據cursor
文檔,如果要進行project
則只需執行以下操作:
.project({'tasks' : 1})
在.toArray
...之前,所以您最終得到:
var result = db.collection('students').find({
'tasks.month': 1,
'tasks.year': 2018
}).project({
'tasks': 1
}).toArray((err, res) => {
console.log(res)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.