Is there a way to figure out the fields/keys in a document while in mongo\'s shell? As an example, let\'s say we have a document like (pseudocode):
{
\"m
To get a list of all fields used in a collection in MongoDB, this is the way I found most straightforward (your mileage may vary :) ):
Create a .js file with the content:
use yourdbname
mr = db.runCommand({
"mapreduce" : "collectionName",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "collectionName" + "_keys"
})
db[mr.result].distinct("_id")
I found out how to do this here (GeoffTech blog)
I ran it from the shell to print the output in the console
mongo < nameOfYourFile.js
or dump the output in a text file:
mongo < nameOfYourFile.js > outputDir\nameOfYourOutputFile.txt
I'm totally new to MongoDb so I hope it does indeed get all fields regardless of use throughout the documents!
(I'm using MongoDb on windows 10, so my console may differ from yours)