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.

 

  1. Create a collection
  2. This command is used to create a collection.

    db.createCollection(“<collectionname>”)

  3. Drop a collection
  4. This command removes a collection from the database.

    db.<collectionname>.drop()

  5. Display records from a collection  
  6. 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>"}]})

  7. Count
  8. 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()

  9. Update records in a collection
  10. 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>)

  11. Delete records from a collection
  12. 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>"]}})

  13. Insert
  14. 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>”}])

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

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

  17. 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>

  18. 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>