Knowledge Quest

Useful technology project advice and development know-how straight from the SharpQuest engineering team.

Top 10 Most Common MongoDB Commands

By Rashmita Nayak  at Nov 02, 2019  0 Comments

Top 10 Most Common MongoDB Commands

Are you new to MongoDB? Then this cheat sheet with the top 10 most common MongoDB commands can be very useful to you.


Create a collection

This command is used to create a collection.


Drop a collection

This command removes a collection from the database.


Display records from a collection  

These commands are used to retrieve records from a collection.

Find all


Find where in


Find by Id


Within a date range

db.getCollection('<collectionname>').find({{ $and: [{"<datefieldname>" : {$gte: ISODate("2018-07-01T00:00:00.000Z")}}, {"<datefieldname>": {$lte: ISODate("2018-08-01T00:00:00.000Z")}} ]}})

With conditional operators



These commands are used to find the count of records with or without a condition.




Update records in a collection

These commands are used to update a field in a collection

Set a standalone field


Set a nested field within a nested array object

db.<collectioname>.update({"_id":"1001"}, {$set:{"<arrayfieldname>$[].<nestedfield1name>.<nestedfield2name>":<newvalue>}}, false <flag to upsert a record. If set to true, it creates a new document if no document matches the query>, true <flag to update multiple records>)

Delete records from a collection

These commands are used to delete records from a collection.

Delete all records


Delete a single record


Delete multiple records



These commands are used to insert records into a collection.

Insert single document

db.<collectionname>.insert({“<field1name>”:”<field1value>”, “<field2name>”:”<field2value>”})

Insert multiple documents

db.<collectionname>.insert([{“<field1name>”:”<field1value1>”}, {“<field1name>”:”<field1value2>”}])

db.<collectionname>.insertMany([{“<field1name>”:”<field1value1>”}, {“<field1name>”:”<field1value2>”}])

Rename a field name

This command is used to rename a field’s name within a collection.

db.<collectionname>.update({"<oldfieldname>": {$exists: true}}, {$rename:{"<oldfieldname>":"<newfieldname>"}}, false, true)

Import JSON array into a collection

This command is used to import bulk records into a collection from a file.

Prerequisite: You would have to download the mongodb server.

Add this to the PATH environment variable, so that the commands can be recognized anywhere:

C:\Program Files\MongoDB\Server\3.6\bin

Command structure:

mongoimport --host <db host name of all replicas where you want to import> --ssl --username <username> --password <password> --authenticationDatabase <admin db name> --db <db name where you want to import into> --collection <collectionname> --type <type of import. E.g. json> --file <filename to import> --jsonArray <optional flag if the data to be imported is a json array>

Export data from collection to a JSON file

This command is used to export all records from a collection into a file

mongoexport --host <db host name of all replicas where you are getting the data from> --ssl --username <username> --password <password> --authenticationDatabase <admin db name> --db <db name where you want to export from> --collection <collectionname> --type <type of import. E.g. json> --out <filename to store the export in>

Join The Discussion

Leave a Reply