获取MongoDB集合中的所有字段名称?
您可以使用MapReduce的概念。首先让我们创建一个包含文档的集合-
> db.getAllFieldNamesDemo.insertOne({"StudentFirstName":"David","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd998e9b50a6c6dd317ad90")
}以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.getAllFieldNamesDemo.find();
这将产生以下输出-
{ "_id" : ObjectId("5cd998e9b50a6c6dd317ad90"), "StudentFirstName" : "David", "StudentAge" : 23 }以下是获取MongoDB集合中所有字段名称的查询-
> myMapReduce = db.runCommand({
"mapreduce" : "getAllFieldNamesDemo",
"map" : function() {
for (var myKey in this) { emit(myKey, null); }
},
"reduce" : function(myKey, s) { return null; },
"out": "getAllFieldNamesDemo" + "_k"
})
{
"result" : "getAllFieldNamesDemo_k",
"timeMillis" : 1375,
"counts" : {
"input" : 1,
"emit" : 3,
"reduce" : 0,
"output" : 3
},
"ok" : 1
}
> db[myMapReduce.result].distinct("_id");这将产生以下输出,显示文件名-
[ "StudentAge", "StudentFirstName", "_id" ]
热门推荐
5 祝福语简短古诗词
10 信耶稣的人祝福语简短
11 鼓掌祝福语简短10字
12 商业mc祝福语大全简短
13 学生节祝福语大全简短
14 锡婚祝福语简短霸气
15 祖国生日祝福语简短英文
16 英语简短祝福语给学生
17 哥哥考大学祝福语简短
18 生日祝福语 简短独特老弟