获取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" ]
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短