Introducing MongoDB Chapter 3
Mongoose is a data modeling technique for Mongo written in Node.js. Every document
inside a Mongoose collection requires a fixed amount of fields. We have to explicitly define
a Schema and adhere to it. An example of a Mongoose schema is:
const UserSchema = new Schema({
name: String,
bio: String,
extras: {}
})
This means that the name and description fields must be a string, whereas the extras can
take a whole JSON object in which we can store nested values as well.
Installing Mongoose
Like any other package, Mongoose can be installed in our project using NPM. Run the
following command in our Terminal inside our express_app folder which we created in
the previous chapter to install Mongoose inside that application:
$ npm install mongoose --save
If this is successfully installed, we should have a line added to our package.json file: