mongodb 获取集合所有记录中曾出现过的字段

小猪肥

关注

阅读 64

2023-04-14


switch to the db you're using and type:

mr = db.runCommand({
  "mapreduce" : "myCollectionName",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; },
  "out": "myCollectionName" + "_keys"
})

once you get result, type:

db[mr.result].distinct("_id")

and you will get a list of fields names.

Get all fields names in a mongodb collection? - Stack Overflow


精彩评论(0)

0 0 举报