仅返回单个属性"_id" [英] return only a single property "_id"
本文介绍了仅返回单个属性"_id"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种方法可以只返回_id
,user_id
和total
而没有items
子文档.
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
}
]
}
推荐答案
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});
您可以执行以下操作排除_id字段:
You can exclude _id field by doing:
db.mycollection.find({}, {"user_id": 1, "_id": 0});
这篇关于仅返回单个属性"_id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文