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

前端 未结 20 1247
执念已碎
执念已碎 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:36

    Not sure this answers the question but I believe it's worth mentioning here. There is one more way for selecting single field (and not multiple) using db.collection_name.distinct();

    e.g.,db.student.distinct('roll',{});

    Or, 2nd way: Using db.collection_name.find().forEach(); (multiple fields can be selected here by concatenation)

    e.g., db.collection_name.find().forEach(function(c1){print(c1.roll);});

    0 讨论(0)
  • 2020-11-22 08:37

    This works for me,

    db.student.find({},{"roll":1})
    

    no condition in where clause i.e., inside first curly braces. inside next curly braces: list of projection field names to be needed in the result and 1 indicates particular field is the part of the query result

    0 讨论(0)
  • 2020-11-22 08:42

    _id = "123321"; _user = await likes.find({liker_id: _id},{liked_id:"$liked_id"}); ; let suppose you have liker_id and liked_id field in the document so by putting "$liked_id" it will return _id and liked_id only.

    0 讨论(0)
  • 2020-11-22 08:44

    From the MongoDB docs:

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

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

    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).

    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.

    0 讨论(0)
  • 2020-11-22 08:44
    db.<collection>.find({}, {field1: <value>, field2: <value> ...})
    

    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: <value>, field2: <value> ... }
    
    The <value> 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

    0 讨论(0)
  • 2020-11-22 08:44

    The query for MongoDB here fees is collection and description is a field.

    db.getCollection('fees').find({},{description:1,_id:0})
    
    0 讨论(0)
提交回复
热议问题