獲取集合中所有鍵的名稱。我想獲得MongoDB集合中所有密鑰的名稱。例如,根據這一點:db.things.insert( { type : ['dog', 'cat'] } );db.things.insert( { egg : ['cat'] } );db.things.insert( { type : [] } );
db.things.insert( { hello : [] } );我想得到唯一的鑰匙:type, egg, hello
3 回答
富國滬深
TA貢獻1790條經驗 獲得超9個贊
$objectToArrray3.4.4$unwind & $group $addToSet
$$ROOT
db.things.aggregate([
{"$project":{"arrayofkeyvalue":{"$objectToArray":"$$ROOT"}}},
{"$unwind":"$arrayofkeyvalue"},
{"$group":{"_id":null,"allkeys":{"$addToSet":"$arrayofkeyvalue.k"}}}])db.things.aggregate([
{"$project":{"arrayofkeyvalue":{"$objectToArray":"$$ROOT"}}},
{"$project":{"keys":"$arrayofkeyvalue.k"}}])
侃侃無極
TA貢獻2051條經驗 獲得超10個贊
mr = db.runCommand({
"mapreduce" : "my_collection",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "my_collection" + "_keys"})db[mr.result].distinct("_id")["foo", "bar", "baz", "_id", ...]- 3 回答
- 0 關注
- 881 瀏覽
添加回答
舉報
0/150
提交
取消
