如何获取MongoDB数据库特定集合中的所有字段名?

只需要两步操作:

  • 先执行:
    mr = db.runCommand({
      "mapreduce" : "具体集合名",
      "map" : function() {
        for (var key in this) { emit(key, null); }
      },
      "reduce" : function(key, stuff) { return null; },
      "out": "具体集合名" + "_keys"
    })

     

  • 然后执行
    db[mr.result].distinct('_id')

     

posted @ 2022-05-12 14:07  eliwang  阅读(1810)  评论(0编辑  收藏  举报