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

前端 未结 20 1282
执念已碎
执念已碎 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);});

提交回复
热议问题