MongoDB Essential Commands

How to insert a new field in mongodb

db.collection1.update({},{$set : {"newfield":1}},false,true)

In the above command, newfield (value = 1) is inserted in the collection - collection 1

How to find unique values

Lets say we want to find unique values for "field1"


How to find by field1 and find unique values for field2

docs = db.collection1.distinct('field2',{'field1':<val>})

Result - docs is a list of values of field2

How to find values using regex

Put your value inside the two forward slashes as shown below...

docs = db.collection1.find({'field1':/<val>/})

To ignore case use "i" after the 2nd forward slash

docs = db.collection1.find({'field1':/<val>/i})

How to update fields with null values

Lets say for a field "package" we want to update the value where 'package" value is null across all the documents in db

db.collection1.update( { "package" : null }, { $set:{ "package" : -1 } }, { multi : true } );

Above command will update the value of "package" whereever it is null to -1 for all the documents.

How to find documents where a specific field does not exist

Lets say, Some of the documents have field1 and some not in a collection. We can find all the documents without field1.

docs = db.collection1.find({'field1':{$exists: false}})

How to rename a field in mongoDB

Lets say we want to update field "package" to "pkg". Use the following command...

db.collection1.update({}, {$rename:{"package":"pkg"}}, false, true)

How to do find documents with logical OR and AND


Above command will find all the documents where package is not equal to empty "AND" not equal to null

We can also use the above command with distinct.


Above command will find all the unique packages where package is not equal to empty "AND" not equal to null

How To Take MongoDB Dump Using mongodump

Lets say we have database name test. We can take the dump of whole database to a directory in mongodb using following...

mongodump --db test -o /home/mongoback
2019-12-26T23:43:40.315-0500	writing test.urls to 
2019-12-26T23:43:40.449-0500	done dumping test.urls (21174 documents)

ls /home/mongoback/test/
urls.bson  urls.metadata.json

rm -rf /home/mongoback

Lets say we want to take the dump of a particular collection in mongodb. Use the -c switch for collection.

mongodump --db test -c urls -o /home/mongoback
2019-12-26T23:48:38.840-0500	writing test.urls to 
2019-12-26T23:48:38.907-0500	done dumping test.urls (21174 documents)

ls /home/mongoback/programcreek/
urls.bson  urls.metadata.json

How To Restore MongoDb Data Using mongorestore

Lets say we want to restore back data from the above dump. We can use mongorestore to do that as shown below.

mongorestore --db test test

Where first test is the name of db that we want to create and second test is the directory which contains our db bson and json files. If successful, you should see following output.

2019-12-27T00:19:52.326-0500  the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-12-27T00:19:52.326-0500  building a list of collections to restore from test dir
2019-12-27T00:19:52.327-0500  reading metadata for test.urls from test/urls.metadata.json
2019-12-27T00:19:52.585-0500  restoring test.urls from test/urls.bson
2019-12-27T00:19:52.961-0500  restoring indexes for collection test.urls from metadata
2019-12-27T00:19:53.425-0500  finished restoring test.urls (21174 documents, 0 failures)
2019-12-27T00:19:53.425-0500  21174 document(s) restored successfully. 0 document(s) failed to restore.

We can also specify a particular collection to import only using collection -c switch in the mongorestore command as shown below.

mongorestore -d test -c urls test/urls.bson

Related Topics:

How to paginate using Mongodb and Django

How to install Mongodb on Centos