Reputation: 6520
I have the structure of documents in the collection like this:
{
"_id" : ObjectId("569190cd24de1e0ce2dfcd62"),
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"rated" : "PG",
"released" : ISODate("1982-06-04T04:00:00Z"),
"runtime" : 113,
"countries" : [
"USA"
],
"awards" : {
"wins" : 2,
"nominations" : 9,
"text" : "2 wins & 9 nominations."
}
}
I'm trying to get the contents of specific fields using the projection and adding a couple of additional parameters. I want to take such keys as: title
, year
, rated
and awards
with the specified values. (and _id
remove)
I write so db.movieDetails.find( {}, {title: 1, year: 2013, rated: "PG-13", _id: 0, "awards.wins": 1 }).pretty()
to get the fields with values, but console displays different parameters:
{
"title" : "Star Trek II: The Wrath of Khan",
"year" : 1982,
"rated" : "PG",
"awards" : {
"wins" : 2,
}
}
I want to have the output like this, for example:
{
"title" : "Star Trek II: The Wrath of Khan",
"year" : 2013,
"rated" : "PG-13",
"awards" : {
"wins" : 0
}
}
Tell me, please, what needs to be corrected in the query to have only my concrete requirements... I appreciate everyone's help.
Upvotes: 5
Views: 4491
Reputation: 478
Filtering should be done inside first parameter of the find() function and projection should be done inside second parameter of the find function.
Upvotes: 2
Reputation: 46491
You can do something like that
db.collection.aggregate([
{
$project: {
title: 1,
_id: 0,
year: 2013,
rated: "PG-13",
"awards.wins": "0"
}
}
])
Will give following output
[
{
"awards": {
"wins": "0"
},
"rated": "PG-13",
"title": "Star Trek II: The Wrath of Khan",
"year": 1982
}
]
Upvotes: 5