MongoDB shell commands (running on Docker) 101

Photo by XPS on Unsplash

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 named dbTest.

  • $ db.createCollection('myFirstCollection'): create a collection 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.

  • $ 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 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.

  • $ db.myFirstCollection.find(): check if newUser can query documents.

  • $ exit: exit.

Dump and Restore

  • $ 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 :)

13