Top 10 Most Common MongoDB Commands
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.
db.createCollection(“<collectionname>”)
Drop a collection
This command removes a collection from the database.
db.<collectionname>.drop()
Display records from a collection
These commands are used to retrieve records from a collection.
Find all
db.getCollection('<collectionname>').find({})
Find where in
db.getCollection('<collectionname>').find({'<fieldname>':{$in:['1','2']}})
Find by Id
db.getCollection(db.getCollection('<collectionname>').find({'_id':'1'}))
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
db.getCollection('<collectionname>').find({$and:[{"_t":{$all:["<field1name>","<field1value>"]}},{"<field2name>":"<field2value>"}]})
Count
These commands are used to find the count of records with or without a condition.
db.getCollection('<collectionname>').count({})
db.getCollection('<collectioname>').count({"<field1name>":"<field1value>"})
db.getCollection('<collectioname>').find({"<field1name>":"<field1value>"}).count()
Update records in a collection
These commands are used to update a field in a collection
Set a standalone field
db.getCollection('<collectionname>').updateMany({"<field1name>":"<field1value>"},{$set:{"<field1name>":"<newvalue>"}})
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
db.getCollection('<collectionname>').deleteMany({})
Delete a single record
db.getCollection('<collectionname>').deleteOne({'_id':'5b634653f370d842b0be465c'}))
Delete multiple records
db.getCollection('<collectionname>').deleteMany({"_t":{$all:["<fieldname>","<fieldvalue>"]}})
Insert
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