return only a single property "_id"

mongodb

Solution

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});

Problem

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 } ] } ```

Original source