更新时间:2023-02-14 09:55:00
来自 MongoDB文档:
投影可以明确包含几个字段.在以下操作中,find()方法返回与查询匹配的所有文档.在结果集中,只有item和qty字段以及默认情况下_id字段返回到匹配的文档中.
A projection can explicitly include several fields. In the following operation, find() method returns all documents that match the query. In the result set, only the item and qty fields and, by default, the _id field return in the matching documents.
db.inventory.find({type:'food'},{item:1,qty:1})
db.inventory.find( { type: 'food' }, { item: 1, qty: 1 } )
在此示例中,来自Mongo的同事们,返回的文档将仅包含item
,qty
和_id
字段.
In this example from the folks at Mongo, the returned documents will contain only the fields of item
, qty
, and _id
.
因此,您应该可以发出如下声明:
Thus, you should be able to issue a statement such as:
db.student.find({}, {roll:1, _id:0})
以上语句将选择学生集合中的所有文档,并且返回的文档将仅返回roll
字段(不包括_id
).
The above statement will select all documents in the students collection, and the returned document will return only the roll
field (and exclude the _id
).
如果我们不提及_id:0
,则返回的字段将是roll
和_id
.默认情况下始终显示"_id"字段.因此,我们需要与roll
一起明确提及_id:0
.
If we don't mention _id:0
the fields returned will be roll
and _id
. The '_id' field is always displayed by default. So we need to explicitly mention _id:0
along with roll
.