MongoDB Shell

Summary: in this tutorial, you will learn about the mongo shell and how to use it to interact with the MongoDB database server.

Introduction to the mongo shell

The mongo shell is an interactive JavaScript interface to MongoDB. The mongo shell allows you to manage data in MongoDB as well as carry administrative tasks.

The mongo shell is similar to the mysql in MySQL, psql in PostgreSQL, and SQL*Plus in Oracle Database.

The mongo shell is included in the MongoDB installation by default. It is installed in the same directory as the server library.

To start the mongo shell, you open the Terminal on macOS and Linux or a Command Prompt on Windows and use the following command:

mongo

Note that you should add the <mongo shell installation directory> to the PATH environment variables in order to use the mongo command directly instead of having to specify the full path to the program or go to the <mongo shell installation directory> first.

The mongo shell automatically connects to the MongoDB running on the local server (localhost) with the default port (27017) therefore you need to make sure that the MongoDB server is up and listening on this port before you start the mongo shell.

You can use the mongo shell as a full-featured JavaScript interpreter and as a MongoDB client.

JavaScript interpreter

The mongo shell is full-featured JavaScript interpreter so that you can execute any JavaScript code like this:

> Math.max(10,20,30); 30
Code language: JavaScript (javascript)

The mongo shell allows you to enter multiline commands. It will detect if the JavaScript statement is complete when you press enter.

If the statement is not complete, the mongo shell will allow you to type on the next line after the three dots (...).

> function add(a, b) { ... return a + b; ... } > add(10,20); 30
Code language: PHP (php)

To cancel the current command and get back to the > prompt, you need to press enter three times.

MongoDB client

The mongo shell is a MongoDB client. By default, the mongo shell connects to the test database on the MongoDB server and assigns the database connection to the global variable called db.

The db variable allows you to see the current database:

> db test

Note that when you type a variable or an expression on the command line, the mongo shell will evaluate it and returns the result.

Besides supporting JavaScript syntax, the mongo shell provides you with useful commands that easily interact with MongoDB database server.

For example, the use command allows you to switch the current database to another.

The following command switches the current database to the bookdb database:

> use bookdb switched to db bookdb
Code language: PHP (php)

Note that you can switch to a database that does not exist. MongoDB will automatically create that database when you first save the data in it.

The mongo shell now assigns bookdb to the db variable:

> db bookdb

Now, you can access the books collection from the bookstore database via the db variable like this:

> db.books bookstore.books
Code language: CSS (css)

Basic CRUD operations

The following section shows you how to create (C), read (R), update (U), and delete (D) a document. These operations are often referred to as CRUD operations.

Note that this section only covers the basic CRUD operations, you will learn about them in detail in the CRUD tutorial.

Create

To add a document to a collection, you use the insertOne() method of the collection.

The following command adds a new document (or a new book) to the books collection:

db.books.insertOne({ title: "MongoDB Tutorial", published_year: 2020 })
Code language: CSS (css)

Output:

{ "acknowledged" : true, "insertedId" : ObjectId("5f2f39fb82f5c7bd6c9375a8") }
Code language: JSON / JSON with Comments (json)

Once you press enter, the mongo shell sends the command to the MongoDB server.

If the command is valid, MongoDB will insert the document and return the result. In this case, it returns an object that has two keys acknowledged and insertedId.

The value of the insertedId is the value of the _id field in the document.

When you add a document to a collection without specifying the _id field, MongoDB automatically assigns a unique ObjectId value to the _id field and add it to the document.

MongoDB uses the _id field to uniquely identify the document in the collection.

Read

To select documents in a collection, you use the findOne() method:

db.books.findOne()
Code language: CSS (css)

Output:

{ "_id" : ObjectId("5f2f3d8882f5c7bd6c9375ab"), "title" : "MongoDB Tutorial", "published_year" : 2020 }
Code language: JSON / JSON with Comments (json)

To format the output, you use the pretty() method like this:

db.books.find().pretty()
Code language: CSS (css)

Output:

{ "_id" : ObjectId("5f2f3d8882f5c7bd6c9375ab"), "title" : "MongoDB Tutorial", "published_year" : 2020 }
Code language: JSON / JSON with Comments (json)

As you can see clearly from the output, MongoDB added the _id field together with other field-and-value pairs to the document.

Update

To update a single document, you use the updateOne() method.

The updateOne() method takes at least two arguments:

  • The first argument identifies the document to update.
  • The second argument represents the updates that you want to make.

The following shows how to update the published_year of the document whose title is "MongoDB Tutorial":

db.books.updateOne( { title: "MongoDB Tutorial" }, { $set: { published_year: 2019 } } )
Code language: PHP (php)

Output:

{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
Code language: JSON / JSON with Comments (json)

How it works.

The first argument identifies which document to update. In this case, it will update the first document that has the title "MongoDB tutorial":

{title: "MongoDB Tutorial"}
Code language: CSS (css)

The second argument is an object that specifies which fields in the document to update:

{ $set: { published_year: 2019 } }
Code language: PHP (php)

The $set is an operator that replaces a field value with a specified value. In this example, it updates the published_year of the document to 2019.

Delete

To delete a document from a collection, you use the deleteOne() method. The deleteOne() method takes one argument that identifies the document that you want to delete.

The following example uses the deleteOne() method to delete a document in the books collection:

db.books.deleteOne({title: "MongoDB Tutorial"});
Code language: CSS (css)

Output:

{ "acknowledged": true, "deletedCount": 1 }
Code language: JSON / JSON with Comments (json)

The output shows that one document has been deleted successfully via the deletedCount field.

Summary

  • The mongo shell is an interactive JavaScript interface to MongoDB. It allows you to manage data in MongoDB as well as perform administrative tasks.
  • CRUD is referred to as four basic functions including creating, reading, updating, and deleting data.
Was this tutorial helpful ?