Introducing MongoDB, a document database

  • MongoDB is a NoSQL, document-based database— storing data in JSON-like (Binary JSON or “BSON”) documents.
    • Unlike relational databases, MongoDB doesn’t require a predefined schema, allowing for more flexibility.
  • Collections in MongoDB are groups of documents, roughly equivalent to tables in relational databases.
    • A collection is automatically created when we attempt to insert a document into it or create an index for it.
  • A MongoDB Database contains collections, similar to a schema in a relational db.
  • MongoDB provides a lot more flexibility. Fields can be freely added or left out in documents. The downside of such a structure is that we do not have a consistent schema for documents. However, this can be solved by using libraries, such as Mongoose,

Other NoSQL implementations

There are other NoSQL database implementations:

  • Key-value stores (e.g. Valkey/Redis)
  • Column-oriented databases (e.g. Amazon Redshift)
  • Graph-based databases (e.g. Neo4j)
  • Document-based (e.g. MongoDB)

Create a new container with a MongoDB server:

docker run -d --name dbserver -p 27017:27017 --restart unless-stopped mongo:6.0.4

Mongoose Object Data Modeling (ODM)

Mongoose is a library that simplifies MongoDB object modeling by reducing the boilerplate code needed to interface with MongoDB.

It also includes common business logic such as setting createdAt and updatedAt timestamps automatically and validation and type casting to keep the db state consistent.