DEV Community

Sid Ali BENTIFRAOUINE
Sid Ali BENTIFRAOUINE

Posted on

Développer une API Rest avec NodeJS, Express et MongoDB: #5 MongoDB et Mongoose

Introduciton to MongoDB and Mongoose

We will use MongoDB as the backend database for our app. You can of course use other solutions to persist your application data e.g. in files, in a relational SQL database, or in another kind of storage mechanism. In this chapter, we will cover the popular MongoDB for database storage.

MongoDB is a NoSQL database. Before we talk about what is a NoSQL database, let ’ s first tall about relational databases so that we can provide a meaningful contrast. If you have not heard of a relational database before, you can think of relational databases like spreadsheets where data is structured and each entry is generally a row in a table. Relational databases are generally controlled with SQL or Structured Query Language. Examples of popular relational databases are MySQL, SQL Server and PostgreSQL.

NoSQL databases are often called non-relational databases, where NoSQL means anything that isn ’ t an SQL (see how it infers the popularity of SQL?). It might seem like NoSQL is a protest over SQL but it actually refers to a database not structured like a spreadsheet, i.e. less rigid than SQL databases.
So, why use Mongo? Firstly, it is popular and that means there is plenty of help online, Secondly, it is mature being around since 2007 and used by companies like eBay, Craigslist and Orange.

Architecture of MongoDB

As mentioned, the architecture of MongoDB is a NoSQL database which stores information in the form of collections and documents. MongoDB stores one or more collections. A collection represents a single entity in our app, for example in an e-commerce app, we need entities like categories, users, products. Each of these entity will be a single collection in our database.

A collection then contain documents. A document is an instance of the entity containing the various relevant fields to represent the document. For example, a product document will contain name, image and price fields. Each field is a key-value pair. Documents look a lot like JSON objects with various properties (though they are technically Binary JSON or BSON). An example of a collection-document tree is shown below:

Database
    → Products collection
        → Product document {
        price: 26,
        title: "Learning Node",
        description: "Top Notch Development book", expiry date: 27-3-2020
        }
        → Product document
        ...

    → Users collection
        → User document {
        username: "123xyz", contact:
        {
        phone: "123-456-7890", email: "xyz@example.com"
        } }
        → User document ...
Enter fullscreen mode Exit fullscreen mode

Machine preparation

In order to connect to a our Database, we need to have one running in our machine. So, follow instruction here: Install MongoDB Community Edition — MongoDB Manual

For visualization

I use Table Plus on my Mac, but the is a good multi-platform GUI for visualizing your Mongo Database: Compass

Mongoose

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

npm install mongoose
Enter fullscreen mode Exit fullscreen mode

Then import Mongoose

const mongoose = require("mongoose")
Enter fullscreen mode Exit fullscreen mode

Do not forget to enable JSON parsing, this lets you access the .body of req:

In your code editor:

app.use(express.json())
Enter fullscreen mode Exit fullscreen mode

bodyParser will let Express parse JSON body of a request.

Connect to our database

In your code editor:

mongoose.connect("mongodb://localhost:27017/test")
Enter fullscreen mode Exit fullscreen mode

Mongoose Model

Lets create a model
- Called: Kitten
- Has these properties:
- name

const Kitten = mongoose.model("Kitten", { name: String })
Enter fullscreen mode Exit fullscreen mode

Just for sake of testing, lets instantly create a new kitten called Le chat

const kittenToSave = new Kitten({ name: "Le chat" })
kittenToSave.save().then((kitten) => res.json(kitten))
Enter fullscreen mode Exit fullscreen mode

The whole file should look like this

const express = require("express")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")

const app = express()

app.use(bodyParser.json())

mongoose.connect("mongodb://localhost:27017/test")

const Kitten = mongoose.model("Kitten", { name: String })
kittenToSave.save().then((kitten) => res.json(kitten))

// TO BE COMPLETED...
app.listen(3000)
Enter fullscreen mode Exit fullscreen mode

The file completed

const express = require("express")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")

const app = express()

app.use(bodyParser.json())

mongoose.connect("mongodb://localhost:27017/test")

const Kitten = mongoose.model("Kitten", { name: String })

// Create
app.post("/kittens", (req, res) => {
  const kittenToSave = new Kitten(req.body)
  kittenToSave.save().then((kitten) => res.json(kitten))
})

// Read All
app.get("/kittens", async (req, res) => {
  Kitten.find()
    .then((kittens) => res.json(kittens))
    .catch(() => res.status(404).end())
})

// Read one by ID
app.get("/kittens/:id", async (req, res) => {
  Kitten.findById(req.params.id)
    .then((kitten) => res.json(kitten))
    .catch(() => res.status(404).end())
})

// Update one by ID
app.put("/kittens/:id", async (req, res) => {
  Kitten.findByIdAndUpdate(req.params.id, req.body)
    .then((kitten) => res.json(kitten))
    .catch(() => res.status(404).end())
})

// Delete one by ID
app.delete("/kittens/:id", async (req, res) => {
  Kitten.findOneAndDelete(req.params.id)
    .then((kitten) => res.json(kitten))
    .catch(() => res.status(404).end())
})

app.get("*", (req, res) => {
  res.status(404).end()
})

app.listen(3000)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)