簡體   English   中英

Mongodb find方法始終返回文檔的所有元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM