获取集合中所有键的名称。我想获得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个赞
$objectToArrray
3.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 关注
- 836 浏览
添加回答
举报
0/150
提交
取消