Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all fields names in a mongodb collection?

Tags:

mongodb

I'm coding a mongoose schema so I need a list of possible field in my collection. Please how can I display all fields names in a specific collection, thank you.

like image 486
user3665192 Avatar asked May 22 '14 13:05

user3665192


1 Answers

switch to the db you're using and type:

mr = db.runCommand({
  "mapreduce" : "myCollectionName",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; },
  "out": "myCollectionName" + "_keys"
})

once you get result, type:

db[mr.result].distinct("_id")

and you will get a list of fields names.

like image 66
user2981029 Avatar answered Nov 15 '22 09:11

user2981029