MongoDB Tutorial menu

MongoDB Getting Started

In this lesson you get a MongoDB database running and send your first commands with the MongoDB Shell.

Two Ways to Get MongoDB

OptionWhat you do
MongoDB Atlas (cloud)Sign up on the MongoDB website, create a free cluster, add a database user and allow your IP address. Nothing to install on your computer.
Local installDownload MongoDB Community Server for Windows, macOS or Linux, and install mongosh (the shell) and MongoDB Compass (a visual tool).

Atlas is the quickest start, and it is what most real apps use. When you add your IP address, you can choose "allow access from anywhere" while learning, but never leave that open for a real project.

Connect with mongosh

mongosh is the MongoDB Shell, a command line where you type JavaScript-style commands.

For a local server:

Example

bash
mongosh

For Atlas, copy the connection string from the Atlas "Connect" screen:

Example

bash
mongosh "mongodb+srv://cluster0.abcde.mongodb.net/" --username ayesha

The prompt shows the current database, such as test>.

Databases

Show all databases:

Example

javascript
show dbs
Output
admin   40.00 KiB
config  60.00 KiB
local   72.00 KiB

Switch to the institute database:

Example

javascript
use institute
Output
switched to db institute

The database does not really exist yet. MongoDB creates it automatically the moment you save the first document in it. That is why institute will not appear in show dbs until then.

The variable db always points to the current database:

Example

javascript
db
Output
institute

Collections

Like databases, collections are created automatically when you first insert into them:

Example

javascript
db.students.insertOne({ name: "Test Student", city: "Karachi" })
Output
{
  acknowledged: true,
  insertedId: ObjectId('6710a1f2c3b4d5e6f7a8b900')
}

Now both the database and the collection exist:

Example

javascript
show collections
Output
students

You can also create a collection yourself, which is useful for adding options later:

Example

javascript
db.createCollection("courses")
Output
{ ok: 1 }

Delete a Collection or Database

Example

javascript
db.students.drop()      // deletes the students collection
db.dropDatabase()       // deletes the current database

Both return true or { ok: 1, dropped: 'institute' } when they succeed. There is no undo, so double-check which database you are in first.

Useful Shell Commands

CommandWhat it does
show dbsLists databases
use nameSwitches database (creates it on first insert)
dbShows the current database
show collectionsLists collections in the current database
db.createCollection("x")Creates a collection
db.x.drop()Deletes a collection
clsClears the screen
exitLeaves mongosh

MongoDB Compass

Compass is MongoDB's free desktop app. You paste the same connection string, then browse databases, edit documents and build queries with buttons. It also has the shell built in at the bottom of the window.

Exercise

Exercise

You type use shop and nothing else. When does the shop database actually get created?

Learn MongoDB with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp