# The MongoDB basics tutorial

> Learn the basics of MongoDB, the schemaless NoSQL document database, from installing it with Homebrew to storing JavaScript objects without a fixed structure.

Author: Flavio Copes | Published: 2018-11-22 | Canonical: https://flaviocopes.com/mongodb/

MongoDB is a NoSQL database. Under the _NoSQL_ umbrella we put all those databases that do not use the SQL language for querying the data.

## Key characteristics of MongoDB

MongoDB is a very [JavaScript](https://flaviocopes.com/javascript/)-friendly database. It exposes a JavaScript API we can use to create databases and collections of objects (called _documents_).

It's _schemaless_, which means you don't need to pre-define a structure for the data before storing it.

In MongoDB you can store any object without having to worry about the particular fields that compose this object and how to store them. You tell MongoDB to store that object.

Data is stored in a format similar to [JSON](https://flaviocopes.com/json/), but enhanced to allow storing more than just basic data types.

## Installation

Let's go ahead and install MongoDB. You could use one of the many cloud providers that offer access to a MongoDB instance, but for the sake of learning, we'll install it ourselves.

I use a Mac, so the installation instructions in this tutorial refer to that operating system.

Open the terminal and run:

```bash
brew tap mongodb/brew
brew install mongodb-community
```

That's it.

The instructions were not too long or complicated, assuming you know how to use the terminal and [how to install Homebrew](https://flaviocopes.com/homebrew/).

The installation tells us this:

```
To have launchd start mongodb now and restart at login:
  brew services start mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
```

You can choose to either launch MongoDB once and have it running forever as a background service in your computer (the thing I prefer), or you can run it just when you need it, by running the latter command.

The default configuration for MongoDB is this:

```
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1
```

Logs are stored in `/usr/local/var/log/mongodb/mongo.log` and the database is stored in `/usr/local/var/mongodb`.

By default there is no access control, anyone can read and write to the database.

## The Mongo Shell

The best way to experiment with MongoDB and starting to interact with it is by running the `mongo` program, which starts the MongoDB shell.

![MongoDB shell startup screen showing version 4.0.3 and connection details](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_07.48.30.png)

You can now enter any command that Mongo understands.

## Create a database

When you start, Mongo creates a database called `test`. Run `db` in the shell to tell you the name of the active database

![Terminal showing db command output displaying test as the current active database](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.01.03.png)

To change the database, just write `use newname` and the `newname` database will be instantly created and the shell switches to using that.

![Terminal showing use something command switching to a new database named something](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.02.30.png)

Use `show databases` to list the available databases:

![Terminal showing show databases command listing admin, config, local and test databases](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.03.31.png)

As you can see, the `something` database is not listed, just because there is no collection yet in it. Let's create one.

## Collections

In MongoDB, a **collection** is the equivalent of a SQL database table.

You create a collection on the current database by using the `db.createCollection()` command. The first argument is the database name, and you can pass an options object as a second parameter.

![Terminal showing db.createCollection cars command returning ok: 1 success response](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.06.28.png)

Once you do so, `show databases` will list the new database, and `show collections` will list the collection.

![Terminal showing show databases and show collections commands displaying something database with cars collection](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.05.13.png)

You can also create a new collection by using it as a property of the `db` object, and calling `insert()` to add an object to the collection:

```js
db.dogs.insert({ name: 'Roger' })
```

![Terminal showing db.dogs.insert command adding Roger document and displaying both cars and dogs collections](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.08.12.png)

## Listing objects in a collection

To show the objects added to a collection, use the `find()` method:

![Terminal showing db.dogs.find command returning Roger document with auto-generated ObjectId](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.09.12.png)

As you can see there is an additional `_id` property for the record we added. That's automatically generated for us by MongoDB.

Now, add more dogs:

```js
db.dogs.insert({ name: 'Buck' })
db.dogs.insert({ name: 'Togo' })
db.dogs.insert({ name: 'Balto' })
```

Calling `db.dogs.find()` will give us all the entries, while we can pass a parameter to filter and retrieve a specific entry, for example with `db.dogs.find({name: 'Roger'})`:

![Terminal showing db.dogs.find results with multiple dog records and filtered search for Roger](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.11.35.png)

The `find()` method returns a cursor you need to iterate on.

There is another method which is handy when you know you'll only get one record, which is `findOne()`, and it's used in the same way. If multiple records match a query, it will just return the first one.

![Terminal showing db.dogs.findOne command examples returning single documents](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.30.40.png)

## Updating records

To update a record you can use the `update()` method on a collection:

![Terminal showing db.dogs.update command changing Togo to Togo2 with nModified: 1 result](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.36.38.png)

## Removing records

You can remove a record calling the `remove()` method on a collection, passing an object to help identify it:

![Terminal showing db.dogs.remove command deleting Togo2 record with nRemoved: 1 result](https://flaviocopes.com/images/mongodb/Screen_Shot_2018-11-02_at_09.37.56.png)

To remove all the entries from a collection, pass an empty object:

```js
db.dogs.remove({})
```
