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]
2
  • 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" Commented Feb 21, 2014 at 10:40
  • possible duplicate of MongoDB Get names of all keys in collection Commented Feb 21, 2014 at 10:52

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.