How to select a single field for all documents in a MongoDB collection?

前端 未结 20 1538
执念已碎
执念已碎 2020-11-22 07:58

In my MongoDB, I have a student collection with 10 records having fields name and roll. One record of this collection is:

{
    \"         


        
20条回答
  •  时光取名叫无心
    2020-11-22 08:44

    db..find({}, {field1: , field2:  ...})
    

    In your example, you can do something like:

    db.students.find({}, {"roll":true, "_id":false})
    

    Projection

    The projection parameter determines which fields are returned in the matching documents. The projection parameter takes a document of the following form:

    { field1: , field2:  ... }
    
    The  can be any of the following:
    
    1. 1 or true to include the field in the return documents.

    2. 0 or false to exclude the field.

    NOTE

    For the _id field, you do not have to explicitly specify _id: 1 to return the _id field. The find() method always returns the _id field unless you specify _id: 0 to suppress the field.

    READ MORE

提交回复
热议问题