Subscribe via RSS Feed

A Quick Primer on MongoDB

May 15, 2013 2 Comments


We have used some of useful commands in MongoDB in our project work. Below is the quick reference of MongoDB commands which can be executed from MongoDB shell. We have tested all the commands from mongodb shell with test data examples.

We can download mongodb from here.

To start mongodb

we should use -

mongod –dbpath <<Our MongoDB Data Directory>>

Now to start the Database from shell -

mongo // to satrt the db

db // to test current database

show dbs // to show all the databases

use userdb // to change to database or to create a new database

// Now to insert data in Collection

user = { username : “firstuser”,password:”firstuser”,email:”[email protected]” } // prepare the data

db.users.insert(user); // insert the data in things collection

show collections; // to see all the collections in mongodb, here we should view the collection “users”  which we have previously created

db.users.find(); // to see all the documents in the collection

db.users.find(0); // find collection by index

db.users.find({username:”firstuser”}); // find doucment in collection by json elements

//Working with cursors in json -

var c = db.users.find();
while ( c.hasNext() ) printjson( c.next() )

// CRUD Operation -

// Insertion of Data

user = { username : “firstuser”,password:”firstuser”,email:”[email protected]” } // prepare the data

//Below command can be used to insert with “_id” or without “_id”

db.users.insert(user); // insert the data in things collection.

//To insert data without “_id” field

db.users.save({username:”thirduser”,password:”thirduser”,email:”[email protected]”});
db.users.find(); // Confirming the operation

// To insert data in collection with “_id” field if the “_id” not found

db.users.save({_id:10,username:”fourthuser”,password:”fourthuser”,email:”[email protected]”});

// To update data in collection with “_id” field if the “_id” found

db.users.save({_id:10,username:”fourthuser1″,password:”fourthuser1″,email:”[email protected]”});

// Same operation update with upsert flag i.e. if data found with “_id” then update operation else insert operation
// follow the opetation – if username with “fifthuser” is found then update operation else insert operation

db.users.update({username:”fifthuser”},{_id:11,username:”fifthuser”,password:”fifthuser”,email:”[email protected]”},{upsert:true});
db.users.find(); // data insertion should be there

db.users.update({username:”fifthuser”},{_id:11,username:”fifthuser1″,password:”fifthuser1″,email:”[email protected]”},{upsert:true});
db.users.find(); // data updation should be there

// Select data from mongodb

db.users.find({username: { $in: [ 10, 11 ] }});
db.users.find({username: { $in: [ "firstuser", "seconduser" ] }});

// A query cursor with find operation -

var myCursor = db.users.find( { username: /.*user.*/ } );
myCursor.forEach(printjson);
// Update field in document
// Set a new field

db.users.update({ _id: 10 },{$set: { ‘username’: ‘sixthuser’ },});
db.users.update({ _id: 10 },{$set: { ‘useraddress’: ‘sixthuser address’ },});
db.users.find();

// Unset a field in the document

db.users.update({ _id: 10 },{ $unset: { useraddress: ‘sixthuser address’ } });
db.users.find();

// Remove document from collection

db.users.remove( { ‘username’ : ‘fifttuser1′ } );

Reference links -
http://docs.mongodb.org/manual/core/update/
http://docs.mongodb.org/manual/core/delete/
http://docs.mongodb.org/manual/core/read/
http://docs.mongodb.org/manual/core/create/

Enter your email address:

Delivered by FeedBurner

Sign Up to read the rest of the content

Email will be used only for updates of our site

No Thanks