MongoDB find with conditional project second param

Viewed 103

Let these commands present in the MongoDB shell:

> use testDB
> db.testCol.insertMany([ { score: 5 }, { score: 8 } ])

Now, I want to fetch all the collecrtion data, but with conditional projection with find. So, let's say I want to include score field in resulted documents, only when the score value is greater than 6.

Something (wrong) like this:

> db.testCol.find({}, { score: { $gt: 6 } })

My target result:

{ "_id" : ObjectId("...") }
{ "_id" : ObjectId("..."), "score": 8 }

How can I do it? Using only find method?

1 Answers
Related