This post shows the syntax of most frequent Monogo DB queries for quick reference.

Create Operations

Create Database

You need to create a collection or insert few records in a database in order to persist a database in mongodb.

  1. Step 1 : use demo will make demo as our current working database.
  2. Step 2 : show dbs will not list demo database, because demo database is not persisted yet.
  3. Step 3 : db.createCollection("person") will create person collection in demo database.
  4. Step 4 : show dbs will list demo database.

> use demo
switched to db demo
> show dbs
local  0.000GB
> db.createCollection("cars");
{ "ok" : 1 }
> show dbs
demo   0.000GB
local  0.000GB
      

Show All Database


> show dbs
demo   0.000GB
local  0.000GB
      

Switch to Specific Database


> use demo
switched to db demo
      

Show Current Database


> db
demo
      

Create Collection


> db.createCollection("person");
      

Insert Operations

Insert Single Docuement


> db.person.insert({ "firstName" : "Hiral", "lastName" : "Patel", "Profession" : "Developer" })
WriteResult({ "nInserted" : 1 })
      

Insert Multiple Documents


>   db.person.insert([
...     { "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician" },
...     { "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" },
...     { "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" },
...     { "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
...   ]);
BulkWriteResult({
  "writeErrors" : [ ],
  "writeConcernErrors" : [ ],
  "nInserted" : 4,
  "nUpserted" : 0,
  "nMatched" : 0,
  "nModified" : 0,
  "nRemoved" : 0,
  "upserted" : [ ]
})
      

Find Operations

Show all documents


> db.person.find()
{ "_id" : ObjectId("58b75b25c94c265ee3b17063"), "firstName" : "Hiral", "lastName" : "Patel", "Profession" : "Developer" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17064"), "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17065"), "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17066"), "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17067"), "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Show all documents in a pretty print


> db.person.find().pretty()
{
  "_id" : ObjectId("58b75b25c94c265ee3b17063"),
  "firstName" : "Hiral",
  "lastName" : "Patel",
  "Profession" : "Developer"
}
{
  "_id" : ObjectId("58b75b41c94c265ee3b17064"),
.....
      

Show all matching documents


> db.person.find({"lastName" : "Trivedi"});
{ "_id" : ObjectId("58b75b41c94c265ee3b17067"), "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Show only one document


> db.person.findOne()
{
  "_id" : ObjectId("58b75b25c94c265ee3b17063"),
  "firstName" : "Hiral",
  "lastName" : "Patel",
  "Profession" : "Developer"
}
      

Project specific columns of a document


> db.person.find({"lastName" : "Patel"}, {"firstName" : 1, "Profession" : 1});
{ "_id" : ObjectId("58b75b25c94c265ee3b17063"), "firstName" : "Hiral", "Profession" : "Developer" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17064"), "firstName" : "Rajesh", "Profession" : "Musician" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17065"), "firstName" : "Sandeep", "Profession" : "Player" }
{ "_id" : ObjectId("58b75b41c94c265ee3b17066"), "firstName" : "Harry", "Profession" : "Actor" }
      

Exclude columns of a document from Projection


> db.person.find({}, {"_id" : 0, "lastName" : 0});
{ "firstName" : "Hiral", "Profession" : "Developer" }
{ "firstName" : "Rajesh", "Profession" : "Musician" }
{ "firstName" : "Sandeep", "Profession" : "Player" }
{ "firstName" : "Harry", "Profession" : "Actor" }
{ "firstName" : "Sandeep", "Profession" : "Electician" }
      

Update Operations

Update document


> db.person.update({"firstName": "Hiral"}, {"Website" : "www.smoothprogramming.com"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.find({}, {_id : 0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Update specific fields of a document


> db.person.update({"firstName" : "Rajesh"}, {$set : {"Website" : "www.rajeshmusic.com"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.find({}, {_id : 0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician", "Website" : "www.rajeshmusic.com" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Update multiple document(s)


> db.person.update({"lastName" : "Patel"}, {$set : {"Gender" : "Male"}}, {multi : true});
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.person.find({}, {_id : 0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician", "Website" : "www.rajeshmusic.com", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player", "Gender" : "Male" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Delete Operations

Delete matching records from a collection


> db.person.remove({"Website" : "www.smoothprogramming.com"})
WriteResult({ "nRemoved" : 1 })
> db.person.find({}, {_id : 0})
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician", "Website" : "www.rajeshmusic.com", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player", "Gender" : "Male" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
      

Delete all records from a collection


> db.person.remove({});
WriteResult({ "nRemoved" : 4 })
> db.person.find({}, {_id : 0})
> 
      

Drop Operations

Drop Collection


> db.person.drop()
true
      

Drop Database

db.dropDatabase() will drop current database.


> use demo
switched to db demo
> db.dropDatabase();
{ "dropped" : "demo", "ok" : 1 }
      

References

MongoDB Documentation