Table of Contents

MongoDB Essential Commands

In this post, I have covered following...

  1. MongoDB find() command
  2. MongoDB update() command
  3. MongoDB distinct() command
  4. MongoDB data dump and restore

MongoDB find() Command

Example: Find all documents which has key1 and value1

db.collection1.find({'key1':'value1'})

Example: Find documents using regex

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

Note: Value inside the two forward slashes is regex expression.

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

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

Example: How to do find documents with logical OR and AND

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

db.collection1.find({"$and":[{"package":{"$ne":""}},{"package":{"$ne":null}}]})

We can also use above command to find distinct documents.

MongoDB update() Command

Example: In the below command, newfield (value = 1) is inserted in the collection - collection 1

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

Example: 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.

Below command will update the value of "package" where ever it is null to -1 for all the documents.

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

Example: 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}})

Example: 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)

MongoDB distinct() Command

Example: Find unique values for "field1"

db.collection1.distinct('field1')

Example: Find all the documents which has unique values for 'field2' but also has 'field1'.

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

Result - docs is a list of values of field2

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

db.collection1.distinct('package',{"$and":[{"package":{"$ne":""}},{"package":{"$ne":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

Related Posts

1
2
3
4