Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view document fields in mongo shell?

Tags:

mongodb

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"
like image 216
Rob Avatar asked May 05 '11 16:05

Rob


People also ask

How do I view documents in MongoDB?

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.

How do I display a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I view databases in Mongo shell?

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.

What field of document is MongoDB on?

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.


4 Answers

Even easier:

Object.keys(db.messages.findOne())
like image 102
Will Fitzgerald Avatar answered Sep 30 '22 07:09

Will Fitzgerald


A for ... in loop should do the trick:

> var message = db.messages.findOne();
> for (var key in message) {
... print(key);
... }
like image 31
Shane Andrade Avatar answered Sep 29 '22 07:09

Shane Andrade


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]);
    }
});

// ------------
like image 10
Manohar Reddy Poreddy Avatar answered Sep 28 '22 07:09

Manohar Reddy Poreddy


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)

like image 2
zanther Avatar answered Sep 29 '22 07:09

zanther