Reputation: 1157
I have student
collection
{
_id: 1,
name: "Student",
tasks: [{ month: 1, year: 2018 }]
}
I need a query which would return only array of tasks:
db.collection('students').find(
{
'tasks.month': 1,
'tasks.year': 2018
},
{
'tasks.$' : 1
}
).toArray((err, res) => {
console.log(res)
})
This return all the documents with all field, including name
and so on ...
Upvotes: 1
Views: 348
Reputation: 18515
The find
would return a cursor
. Based on the cursor
documentation if you want to project
then you can simply do:
.project({'tasks' : 1})
before your .toArray
... so you end up with:
var result = db.collection('students').find({
'tasks.month': 1,
'tasks.year': 2018
}).project({
'tasks': 1
}).toArray((err, res) => {
console.log(res)
})
Upvotes: 1