我有一批要处理的数据类似:
/* 1 */
{
...
"data" : [
{
"status" : "OK",
"name" : "Administrator",
"description" : "管理计算机(域)的内置帐户"
}
],
"uptime" : ISODate("2017-06-29T06:42:40.934Z")
}
/* 2 */
{
...
"data" : [
{
"address" : "0.0.0.0:135",
"pid" : "920",
"proto" : "TCP"
},
{
"pid" : "4",
"proto" : "TCP",
"address" : "0.0.0.0:445"
}
],
"uptime" : ISODate("2017-07-11T02:48:54.271Z")
}
如上所示,每个 data 内是一个字典的列表,字典内的 key 有可能不尽相同。我想把 data 内部的所有 key 值取出来,生成这样的 list:
["pid", "proto", "address", "status", "name"...]
思考想用 map-reduce 实现:
mr = db.runCommand({
"mapreduce" : "info",
"map" : function() {
for (var item in this.data) {
for (var key in item){
emit(key, null);
}
}
},
"reduce" : function(key, stuff) { return null; },
"out": "keys"
})
db[mr.result].find()
但是出来的数据并非我想要的数据。看了好久的 map-reduce 文档,都没改好。对 mongodb 实在不熟,特来求教。
1
hawkflying 2017-09-24 20:58:06 +08:00
转自: https://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection
亲测: 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" }) |