21
loading...
This website collects cookies to deliver better user experience
$ 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
.
$ 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
.
$ 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.
$ 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.
$ 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.
$ 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.
$ 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.