0

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]
Filburt
  • 17,626
  • 12
  • 64
  • 115
Sameer Rathoud
  • 133
  • 3
  • 8
  • Can I suggest a re-wording. "I want to find **all** of the field names that are present in all of the documents in my collection" – Neil Lunn Feb 21 '14 at 10:40
  • possible duplicate of [MongoDB Get names of all keys in collection](http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection) – Neil Lunn Feb 21 '14 at 10:52

1 Answers1

1

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);
Saneesh kunjunni
  • 548
  • 5
  • 17