例如我要获得所有的name字段集合['bob', 'ahn', 'abc'],该怎么写sql
{
"_id" : 2,
"name" : "bob",
"age" : 42,
"type" : 1,
"status" : "A",
"finished" : [ 11, 25 ],
"badges" : [ "green" ]
}
{
"_id" : 3,
"name" : "ahn",
"age" : 22,
"type" : 2,
"status" : "A",
"finished" : [ 6 ],
"badges" : [ "blue", "red" ]
}
{
"_id" : 6,
"name" : "abc",
"age" : 43,
"type" : 1,
"status" : "A",
"finished" : [ 18, 12 ],
"badges" : [ "black", "blue" ]
}
巴扎黑2017-04-17 15:03:44
No matter which method is used, the result the database will give you will only be:
[{name: 'bob'},
{name: 'ahn'},
{name: 'abc'}]
If you are under shell, you can use JS script to convert:
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. But this part of the work is not actually done by the database for you, but by the conversion done by the driver on the app side. The database is an important resource, and resource-consuming behaviors should be placed on the application side as much as possible without affecting performance. Because horizontal expansion of applications is easier and less expensive than databases.