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):
{
"message": "Hello, world",
"from": "hal",
"field": 123
}
I'd like to run a command in the shell that returns the list of fields/keys in that document. For instance, something like this:
> var message = db.messages.findOne()
> message.keys()
... prints out "message, from, field"
In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
In MongoDB, you can use the show dbs command to list all databases on a MongoDB server. This will show you the database name, as well as the size of the database in gigabytes. You can select any database using the use statement and work on it.
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. This also applies to documents inserted through update operations with upsert: true.
Even easier:
Object.keys(db.messages.findOne())
A for ... in
loop should do the trick:
> var message = db.messages.findOne();
> for (var key in message) {
... print(key);
... }
Other answers are correct.
However, as I am completely new, I didn't understand where & how the above commands need to be executed.
Below helped, from my github.
On Windows: Run this code in a command prompt (cmd).
On Mac or Linux: Run this code in a terminal window.
// ------------
// start mongo client
mongo
// ------------
// list all databases
show dbs
// NOTE: assume one of the databases is myNewDatabase
// use the 'myNewDatabase' database
use myNewDatabase
// ------------
// show all collections of 'myNewDatabase' database
show collections
// NOTE: assume one of the collections is 'myCollection'
// show all documents of 'myCollection' collection
db.myCollection.find()
// ------------
// field keys
Object.keys(db.myCollection.findOne());
// values
db.myCollection.find().forEach(function(doc) {
for (field in doc) {
print(doc[field]);
}
});
// ------------
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With