How to view data in MongoDB

I am sharing my experiences with using Mongo Shell to view data in MongoDB

In order to start the Mongo Shell
1. Go to your Mongo Installation directory/bin and type mongo









For a sample collection "person" and database "test"

To display the database that is currently used : db
To switch to a different database : use test
To find a collection: db.person.find()
To display the collection result in a nice format: db.person.find().pretty()

For a sample collection "person"

{
    "_id": ObjectId("55410c22131dc20245e953a2"),
    "address": {
                           "city" : "Houston",
                            "state" : "Texas"
                         }
}

To search for a field : db.person.find({"address.city":"Houston"})

Remove a collection from database : db.person.drop()
1. This also removes the indices associated with the collection
2. This method doesn't accept any arguments
3. 

How to get the data in a Mongo DB collection into a document.1. Go to MongoDB installation directory/bin
2. Type the following

C:\Program Files\MongoDB 2.6 Standard\bin>mongo localhost:27017/test -eval "printjson(db.person.find().toArray())"
> C:/tt.txt

where test is the database.

For more info on MongoShell







No comments:

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days