Notes from Junaid

Notes from Junaid

My Mongodb Cheatsheet

My quick cheatsheet to quickly lookup commands for the mongo shell.

Subscribe to my newsletter and never miss my upcoming articles

My quick cheatsheet to quickly lookup commands for the mongo shell.

Installation on Ubuntu 20.04

mongodb-org package is the officially maintained one, instructions are on the website

mongodb compass is the GUI to manage Databases

Starting MongoDB

sudo service mongod start

sudo service mongod stop

sudo service mongod restart

Run mongo to start the shell

mongo shell commands

Databases:

  1. use <database_name> to switch to use/create a database
  2. show dbs to show the databases
  3. db.dropDatabase() to drop the Database

We can create multiple databases

Collections:

  1. show collections to show the collections in the current database
  2. db.createCollection("<name>") to add a collection.
  3. db.<collection_name>.drop() to drop a collection

Documents:

  1. db.<collection_name>.insertOne(<JSON_object>) to insert a document in the collection
  2. db.<collection_name>.insertMany(<JSON_array>) to insert many documents in the collection
  3. db.<collection_name>.remove({}) will remove the doc with the following properties

Querying the Database:

  1. db.<collection_name>.find() to view all the documents in the collection
  2. db.<collection_name>.find("{}") to view all the documents in the collection with the following properties
  3. db.<collection_name>.find("{})".count() to view the number of documents in the collection with the following properties
  4. db.<collection_name>.find("{}".sort() to sort all the documents in the collection with the following properties
  5. db.<collection_name>.find().sort({ title:1 }) to sort in ascending order in terms or title, 1 for ascending and -1 for descending.
  6. db.<collection_name>.find("{}".limit(2) to limit the view to 2 documents in the collection with the following properties
  7. db.<collection_name>.find().pretty() to view all the documents in a formatted way
  8. db.<collection_name>.find( { $text: { $search: "\" Post One\""}} ) will search for the text "Post One" in the entire collection and return the results.

You can replace <collection_name> with <database_name> to view everything in the databse.

We can also do things like .sort().count().pretty() ⇒ we can chain functions.

Updating Existing Documents

  1. db.<collection_name>.update({original_doc},{updated_doc}) will replace the original doc with the updated doc
  2. db.<collection_name>.update({original_doc},{updated_doc}, {upsert: true}) will replace the original doc with the updated doc and if the original doc is not present then it will insert the updated doc
  3. db.<collection_name>.update({original_doc}, { $set: {properties to be updated}}) will update/add only the properties passed in the $set property

other properties include

  • $inc ⇒ to increment ; properties passed in the increment object will be incremented by the value passed
  • $rename ⇒ to rename a particular field; properties passed in here will be renamed to the value

Find more commands here:

 
Share this