Reputation: 1053
I would like to know if there is a way to return only the _id
, user_id
and total
without the items
subdocument.
{
"_id" : 122,
"user_id" : 123456,
"total" : 100,
"items" : [
{
"item_name" : "my_item_one",
"price" : 20
},
{
"item_name" : "my_item_two",
"price" : 50
},
{
"item_name" : "my_item_three",
"price" : 30
}
]
}
Upvotes: 35
Views: 38144
Reputation: 36300
What I was looking for is returning a list of ids, so return only _id but as an array for all documents. I did it as such:
db. mycollection.distinct("_id", {})
The first parameter is your distict fields, and second is your query. More on distinct here. Hopefully this helps someone with a similar requirement. Cheers!
Upvotes: 31
Reputation: 102763
The second parameter of find lets you select fields. So you can use this (note that the _id field is always selected anyway):
db.mycollection.find({}, {"user_id": 1, "total": 1});
You can also exclude certain fields, so this would be equivalent:
db.mycollection.find({}, {"items": 0});
You can exclude _id field by doing:
db.mycollection.find({}, {"user_id": 1, "_id": 0});
Upvotes: 43