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
- Drop a collection
- Display records from a collection
- Find all
- Find where in
- Find by Id
- Within a date range
- With conditional operators
- Count
- Update records in a collection
- Set a standalone field
- Set a nested field within a nested array object
- Delete records from a collection
- Delete all records
- Delete a single record
- Delete multiple records
- Insert
- Insert single document
- Insert multiple documents
- Rename a field name
- 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:
- Command structure:
- Export data from collection to a JSON file
This command is used to create a collection.
db.createCollection(“<collectionname>”)
This command removes a collection from the database.
db.<collectionname>.drop()
These commands are used to retrieve records from a collection.
db.getCollection('<collectionname>').find({})
db.getCollection('<collectionname>').find({'<fieldname>':{$in:['1','2']}})
db.getCollection(db.getCollection('<collectionname>').find({'_id':'1'}))
db.getCollection('<collectionname>').find({{ $and: [{"<datefieldname>" : {$gte: ISODate("2018-07-01T00:00:00.000Z")}}, {"<datefieldname>": {$lte: ISODate("2018-08-01T00:00:00.000Z")}} ]}})
db.getCollection('<collectionname>').find({$and:[{"_t":{$all:["<field1name>","<field1value>"]}},{"<field2name>":"<field2value>"}]})
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()
These commands are used to update a field in a collection
db.getCollection('<collectionname>').updateMany({"<field1name>":"<field1value>"},{$set:{"<field1name>":"<newvalue>"}})
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>)
These commands are used to delete records from a collection.
db.getCollection('<collectionname>').deleteMany({})
db.getCollection('<collectionname>').deleteOne({'_id':'5b634653f370d842b0be465c'}))
db.getCollection('<collectionname>').deleteMany({"_t":{$all:["<fieldname>","<fieldvalue>"]}})
These commands are used to insert records into a collection.
db.<collectionname>.insert({“<field1name>”:”<field1value>”, “<field2name>”:”<field2value>”})
db.<collectionname>.insert([{“<field1name>”:”<field1value1>”}, {“<field1name>”:”<field1value2>”}])
db.<collectionname>.insertMany([{“<field1name>”:”<field1value1>”}, {“<field1name>”:”<field1value2>”}])
This command is used to rename a field’s name within a collection.
db.<collectionname>.update({"<oldfieldname>": {$exists: true}}, {$rename:{"<oldfieldname>":"<newfieldname>"}}, false, true)
C:\Program Files\MongoDB\Server\3.6\bin
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>
- 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>