Skip to content

2. MongoDB

MongoDB is a document-oriented database, based on storing its data structures in JSON-type documents with a dynamic schema. Although it began being developed by the 10gen company, today it is an open source project, with a large user community.

A MongoDB server can contain several databases, and each of them made up of a set of collections, which we could compare to the tables of a relational database. Each collection stores a set of JSON documents, made up of key-value attributes, which would be the records of a relational database.

Broadly speaking, we could establish the following comparisons:

Relational model MongoDB
Relational DB Document-oriented DB
Table Collection
Record/Row JSON Documents
Table columns and values Key:Value pairs

2.1. Setup and first run

MongoDB covers a wide range of possibilities, from local database servers to cloud databases. On its website https://www.mongodb.com, we can discover the different products and services that are offered, among which we find:

  • MongoDB DB server, with its two versions, the Community, free and community version, and the Enterprise, its commercial version aimed at the business world and with additional features that improve performance and support. In addition to the server itself, the server prepared for use in containers is also offered on the web, through Kubernetes operators. The server is available on various platforms: Linux, Solaris, MacOS X and Windows.
  • We can use the mongo shell or compass or Robo3T graphical as a client, installed in our system
  • MongoDB Atlas, the MongoDB platform in the cloud (DBaaS or DataBase as a Service), which allows its deployment in services such as AWS, Azure or Google Cloud.
  • Realm, a data service designed for mobile and web applications, and which includes, in addition to DB in the cloud, several fully managed backend services.

We are going to use, as current course, all servers via docker containers. We could download docker image via:

Bash
docker pull mongo

and then create a server in a working container with

Bash
1
2
3
4
5
docker run --name mongodb-srv 
    -e MONGO_INITDB_ROOT_USERNAME="root" 
    -e MONGO_INITDB_ROOT_PASSWORD="toor" 
    -p 27017:27017 
    -d mongo

We can connect with compass to work with graphic interface