Full-stack Web Technologies

CHAPTER 1
MongoDB

Setting up a MongoDB instance

Get the databases/mongodb sample

degit full-stack-bcn/samples/databases/mongodb mongodb

Change into the mongodb/db directory and launch Docker Compose

docker-compose up -d

Connect to the Mongo instance:

docker exec -it db-mongo-1 mongosh -u admin -p fullstack

Collections and Documents

A database in MongoDB is a set of collections, each collection being a set of documents. Every document is a limited-size JSON-like object, which has a free JSON-like recursive structure.

image

Every object in MongoDB has an ID, which is always implicit and has the special name _id.

image

The Mongo Shell can be invoked with mongosh and it is a Javascript REPL, just like Node, but with extra commands. A global db object represents the open database.

MongoShell commands

Show general help:

help

Show database help (all db methods with signatures):

db.help()

Show collection help:

db.todos.help()

(Just like with Node, pressing TAB shows available methods.)

Non-Javascript commands

Some commands are not Javascript, but very convenient:

Show all databases:

show dbs

Switch to a different database:

use foo

Show collections in this database:

show collections

Executing script

To execute a script with Mongo Shell, you can pass it as the first arguments, just like with Node:

mongosh script.js

Once inside mongosh, you can load a script with:

load("script.js")

Mongo Compass

Mongo Compass is a complete UI (probably made with Electron) which makes it easier to interact with MongoDB. You can connect either locally or on remote databases (especially Mongo Atlas).