获取mongodb集合中的所有字段名称? [英] Get all fields names in a mongodb collection?

查看:183
本文介绍了获取mongodb集合中的所有字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 mongoose 模式,所以我需要我的集合中可能的字段列表.请问如何显示特定集合中的所有字段名称,谢谢.

I'm coding a mongoose schema so I need a list of possible field in my collection. Please how can I display all fields names in a specific collection, thank you.

推荐答案

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

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.

这篇关于获取mongodb集合中的所有字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆