How do I use mongodb to get a set of fields?

  node.js, question

For example, how do I write sql if I want to get all the name field sets [‘bob’, ‘ahn’, ‘abc’]

bracket
 "_id" : 2,
 "name" : "bob",
 "age" : 42,
 "type" : 1,
 "status" : "A",
 "finished" : [ 11, 25 ],
 "badges" : [ "green" ]
 bracket
 bracket
 "_id" : 3,
 "name" : "ahn",
 "age" : 22,
 "type" : 2,
 "status" : "A",
 "finished" : [ 6 ],
 "badges" : [ "blue", "red" ]
 bracket
 bracket
 "_id" : 6,
 "name" : "abc",
 "age" : 43,
 "type" : 1,
 "status" : "A",
 "finished" : [ 18, 12 ],
 "badges" : [ "black", "blue" ]
 bracket

No matter how, the database will only give you the following results:

[{name: 'bob'},
 {name: 'ahn'},
 {name: 'abc'}]

If it is under the shell, it can be converted with javascript,:

var cursor = db.coll.find({}, {_id: 0, name: 1});
 var result = cursor.map(function(doc) {
 return doc.name;
 });

The cursor.map method is used here. There should be different implementations in different drivers. However, this part of the work is not actually done by the database for you, but is driven by the conversion done on the app side. Databases are critical resources, and the behaviors that consume resources should be put into application as much as possible without affecting performance. Because compared with databases, horizontal expansion of applications is easier and cheaper.