The MongoDB Book

How to start mongo up in docker and connect to it

// (1) Download a docker image that holds a mongodb database
$ docker pull mongo

// (2) Run a docker container that has the mongo database in it
$ docker run -d -p 27017-27019:27017-27019 --name mongodb mongo

// (3) Connect to the mongo database in the docker container
$ docker exec -it mongodb mongo

How to see what the active database is

> db

How to switch to a new (or existing) database

> use natours-test

How to insert a document into a collection in the active database

// db - the active database
// tours - the collection (Will be created if it does not exist)
// insertOne - the function used to add a single document to the collection
// {a: "ape"} - the document to insert into the collection

> db.tours.insertOne({ a: "ape" })

How to get a list of all the databases that are available

How to see what collections are available in the active database

How to see what documents are in a collection

How to create an AND query

How to create an OR query

How to get a projection from your results

How to update a document in a collection

How to replace a document in a collection

How to delete a document from a collection

Last updated