发布于 2年前

MongoDB使用MapReduce获取Collection所有的键

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")
©2020 edoou.com   京ICP备16001874号-3