Map Reduce의 개념을 사용할 수 있습니다. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> 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" ]