MongoDB使用MapReduce获取Collection所有的键

时间:2022-06-01 17:14:56

MongoDB作为一个文档数据库,它不像关系型数据那样有固定的schema。有时我们需要获取Collection集合里所用的键。

使用MapReduce获取Collection的键

keys= db.runCommand({
  "mapreduce" : "test",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; }, 
  "out": "test_keys"
})

对结果做唯一区分

db.test_keys.distinct("_id")