MongoDB shell commands (running on Docker) 101
Tags: mongodb, docker, database, beginners
Published: 2021-07-05 21:29:20

_Photo by <a href="https://unsplash.com/@xps?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">XPS</a> on <a href="https://unsplash.com/s/photos/database?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>_
  
This tutorial shows how you can start and understand a bit more about MongoDB. I create this cheat sheet because I need to work on a project that uses MongoDB, and doing the steps below gave me an overview of how MongoDB works. Of course, there are many more functionalities, but here I just focused on creating a Docker container, databases, collections, documents, adding users, performing some queries, dumping, and restoring databases.

I've decided to use Docker because it's easier to start and clean up the system after doing some tests. 

### Creating and Accessing Container

- `$ docker run -d -p 27017:27017 --name MONGO_CONTAINER mongo:latest`: run MongoDB container in detached mode (running in background).

- `$ docker ps`: list all running instances. **MONGO_CONTAINER** container should be running.

- `$ docker exec -it MONGO_CONTAINER mongo`: execute `mongo` command in the **MONGO_CONTAINER** directly in your shell. You could also run `$ docker exec -it MONGO_CONTAINER bash` and after, `$ mongo`.

### Creating Database, Collection, and Items

- `$ use dbTest`: create a [database](https://docs.mongodb.com/manual/core/databases-and-collections/#databases) named **dbTest**.

- `$ db.createCollection('myFirstCollection')`: create a [collection](https://docs.mongodb.com/manual/core/databases-and-collections/#collections) named **myFirstCollection**.

- `$ db.myFirstCollection.insert({item: "card1", qty: 10})`: add a document in the **myFirstCollection** colection.

- `$ db.myFirstCollection.insert({item: "card2", qty: 20})`: add a second document in the **myFirstCollection** colection.

- `$ db.myFirstCollection.find()`: select documents in the **myFirstCollection** colection. It should display the two created documents with the `_id` property. [db.collection.find() reference](https://docs.mongodb.com/manual/reference/method/db.collection.find/).

- `$ db.myFirstCollection.find({ item: "card1" })`: it returns only the document whose `item` property is `card1`.

- `$ db.myFirstCollection.find({ qty: { $gt: 4 } })`: it returns all documents whose `qty` property is greater than `4`, in our case, all documents.

- `$ db.myFirstCollection.find({ qty: { $gt: 14 } })`: it returns all documents whose `qty` property is greater than `14`, in our case, only `card2`.

### Exiting MongoDB Shell and Accessing Again

- `$ exit`: exit Docker MongoDB shell.

- `$ docker stop MONGO_CONTAINER`: stop **MONGO_CONTAINER**.

- `$ docker ps`: check if **MONGO_CONTAINER** isn't running.

- `$ docker start MONGO_CONTAINER`: start **MONGO_CONTAINER** again.

- `$ docker exec -it MONGO_CONTAINER mongo`: access again.

- `$ show dbs`: list all databases.

- `$ use dbTest`: use **dbTest**.

- `$ db.myFirstCollection.find()`: check if the created documents is still there.

### Creating Users

- `$ show roles`: list all [roles](https://docs.mongodb.com/v5.0/reference/built-in-roles/) that you can give to new users.

- `$ db.createUser({ user: "newUser", pwd: "123123123", roles: [{ role: "readWrite", db: "dbTest" }] })`: create new user named **newUser**.

- `$ show users`: list all users.

- `$ exit`: exit.

### Access Using Specific User

- `$ docker exec -it MONGO_CONTAINER  mongo --username newUser --password 123123123 --authenticationDatabase dbTest`: access with the **newUser**.

- `$  db.runCommand({connectionStatus : 1})`: [check the current user](https://stackoverflow.com/questions/21414608/mongodb-show-current-user).

- `$ db.myFirstCollection.find()`: check if **newUser** can query documents.

- `$ exit`: exit.

### Dump and Restore

[Full reference here.](https://dev.to/mkubdev/mongodump-and-mongorestore-with-docker-39m7)

- `$ docker exec MONGO_CONTAINER sh -c "mongodump --db dbTest --archive" > db.dump`: dump **dbTest**. If you omit `--db dbTest`, then you'll dump all databases.

- `$ docker run -d -p 27018:27017 --name MONGO_CONTAINER_2 mongo:latest`: create new MongoDB container.

- `$ docker exec -i MONGO_CONTAINER_2 sh -c "mongorestore --db dbTest --archive" < db.dump`: restore **dbTest**  in **MONGO_CONTAINER_2** container.

- `$ use dbTest`: use **dbTest**. 

- `$ db.myFirstCollection.find()`: check if the documents are in the new MongoDB instance.

- `$ exit`: exit.

### Clear

- `$ docker stop MONGO_CONTAINER MONGO_CONTAINER_2`: stop both containers.

- `$ docker ps -a`: check all instances, even the stopped ones.

- `$ docker rm MONGO_CONTAINER MONGO_CONTAINER_2`: remove both containers.

- `$ docker ps -a`: check if the MongoDB instances were deleted.

## Conclusion

Probably you won't use MongoDB this way in your production projects, but it's an illustrative step-by-step to understand the basics of the MongoDB engine. I hope you like it :)