且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取mongodb集合中的所有字段名称?

更新时间:2023-02-18 19:06:29

切换到您正在使用的数据库并输入:

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"
})

得到结果后,输入:

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

你会得到一个字段名称列表.

and you will get a list of fields names.