简体   繁体   中英

How to find number of distinct fields in a collection in mongodb

As MongoDB provides the flexibility to store the unstructured data,

Is there any way in mongodb C# driver, I can find the number of distinct fields name from a collection.

I mean to say

{
     "_id" : ObjectId("52fb69ff1ecf0322f0ab3129"),
     "Serial Number" : "1",
     "Name" : "Sameer Singh Rathoud",
     "Skill" : "C++",
     "City" : "Pune",
     "Country" : "India" 
}
{
     "_id" : ObjectId("52fb69ff1ecf0322f0ab312a"),
     "Serial Number" : "2",
     "Name" : "Prashant Patil",
     "DOB" : "31/07/1978",
     "Location" : "Hinjewadi",
     "State" : "Maharashtra",
     "Country" : "India" 
}

I want to get [_id, Serial Number, Name, DOB, Skill, City, State, Country]

i also faced this issue. If you till not got proper solution or for new person who searching solution for this kind of question they can use this.

var keys = [];
db.Entity.find().forEach(function(doc){
 for (var key in doc){ 
     if(keys.indexOf(key) < 0){
        keys.push(key);
     }
 }
});
print(keys);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM