Answer the question
In order to leave comments, you need to log in
How to get a list of fields in a MongoDB document?
There is a Mongo base, there is a certain document with collections. Is there any possibility, preferably through mongoengine, to get the list of field names?
For example
class User(db.Document):
login = db.StringField()
name = db.StringField()
aboutme = db.StringField()
at the output get the list ['login', 'name', 'aboutme']
Answer the question
In order to leave comments, you need to log in
mr = db.runCommand({
"mapreduce" : "my_collection",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "my_collection" + "_keys"
})
db[mr.result].distinct("_id")
No. MongoDB is a schemaless database, completely different documents can be stored in the same collection (another thing is that in practice this is not necessary).
You can iterate the entire (or not all) collection and see which fields of which type are encountered.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question