Skip to main content

Database

In a full-stack web application, the database is an integral part of the stack responsible for storing, retrieving, and managing data over time. It's the piece that allows our web application to retain information between user sessions.

While the React client handles user interaction and the node.js server processes the requests, the database holds the data - user accounts, posts, products, items, transactions - in a structured format.

info

The database persists the data over sessions.

Why noSql?

Web applications often use non-relational databases such as MongoDB because they offer flexibility, scalability, and performance advantages that fit modern, web development needs.

In a relational database such as sql, data is structured in tables - rows and columns. To avoid duplicate data, the data is stored in related tables. In a non-relational database, data is stored in documents. Document databases use JSON documents. Data is stored in key-value pairs. With the document model, we can store related data in the same document. This is an efficient model for data that will be accessed together - it is stored together in the same document.

MongoDB

Mongodb Logo
MongoDB

We'll use a popular document database - MongoDB. MongoDB uses a document format that directly matches JSON. In our React/Next.js/Node.js stack, we can send and receive JSON between the client, server and database without a translation process.

In Mongo, each document can have its own structure, so you can add or remove fields without redesigning tables.

This is ideal for web applications where data structures may change (for example, user profiles that gain new fields over time).

ODM - Mongoose

Mongoose ODM
Mongoose ODM - Object Data Mapper

Web applications work with objects in memory, while MongoDB stores data as BSON documents (binary JSON). They are similar, but are not exactly the same — and managing database operations manually can quickly become repetitive and error-prone.

An Object Data Mapper (ODM) bridges this gap by providing a layer of abstraction between your application code and the database. An ODM (Object Data Mapper) provides:

  1. Schema Definition

You define a schema for your data models, specifying field names, types, and validation rules. This adds structure and reliability to otherwise “schema-less” MongoDB collections.

  1. Data Validation

Before saving to the database, Mongoose automatically checks that required fields and data types are valid — reducing runtime errors and bad data.

  1. Object Mapping

The ODM automatically converts between:

JavaScript objects in your code  <--->. BSON documents in MongoDB

That means you can work with JavaScript objects — calling methods like .save(), .find(), .updateOne() — instead of constructing query syntax by hand.

  1. Query Builder

ODMs offer clean, chainable syntax for queries:

const results = await Item.find({ owner: "Diane" }).sort({ title: 1 });

This replaces raw MongoDB commands with readable, type-safe method calls.

info

An Object Data Mapper like Mongoose gives your JavaScript code a structured, object-oriented interface for reading and writing data.

Mongoose is a Object Data Modeling (ODM) library for MongoDB distributed as an npm package. Like other frameworks, Mongoose simplifies your data access code by managing the database access details.

Using Mongoose involves defining object schemas. This is straightforward since the data is stored as objects in MongoDB.

Mongodb Object Data Mapping
Mongoose manages relationships between data, validates schema, and translates between objects in code and objects in MongoDB.