V
V
Vladimir2015-09-29 11:49:14
MongoDB
Vladimir, 2015-09-29 11:49:14

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

2 answer(s)
P
Pavel, 2015-09-29
@FonVald

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")

K
Konstantin Kitmanov, 2015-09-29
@k12th

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 question

Ask a Question

731 491 924 answers to any question