Adding Mongoose
As the application get more complex it would be useful to make use of the library Mongoose.
Mongoose provides several features that make it preferable to using plain MongoDB:
Schema Definition: Mongoose allows you to define a schema for your documents using a flexible and expressive schema definition syntax. With schemas, you can define the structure, data types, validation rules, default values, and more for your documents. This helps in maintaining consistent data structure and enforcing data integrity.
Data Validation: Mongoose provides built-in validation capabilities for your document fields. You can specify validation rules such as required fields, minimum and maximum values, regular expressions, custom validation functions, and more. Mongoose automatically validates the data before saving it to the database, ensuring that the data meets the defined criteria.
Middleware: Mongoose supports middleware functions that can be executed before or after specific operations like saving, updating, or removing documents. This allows you to add custom logic or perform additional tasks during these operations. Middleware functions provide flexibility and extensibility to your data operations.
Object-Document Mapping (ODM): Mongoose acts as an Object-Document Mapping library, allowing you to work with MongoDB using JavaScript objects. It abstracts away the low-level MongoDB queries and provides a higher-level API that simplifies database operations. Mongoose helps in writing cleaner and more maintainable code by providing a structured and intuitive way to interact with the database.
Ecosystem and Community: Mongoose has a large and active community, which means there are plenty of resources, tutorials, and plugins available. It integrates well with popular Node.js frameworks like Express and provides additional functionality through plugins. The ecosystem around Mongoose can help you speed up development and solve common problems efficiently.
Overall, using Mongoose with MongoDB offers a more productive and convenient way to work with MongoDB databases by providing features like schema definition, data validation, middleware, and an ODM layer. It simplifies database operations, improves code readability, and helps maintain data consistency and integrity.
Install Mongoose
Use npm install
to add Mongoose to the project:
Adding a Schema
Create a file in a new models folders saved as models/film.js
.
First import the necessary dependencies:
Next define the film schema:
Here, a new Mongoose schema is defined using the mongoose.Schema
constructor. The schema represents the structure of the documents that will be stored in the MongoDB collection.
The schema defines several fields for a film document, including filmCertificate
, filmTitle
, filmDescription
, filmImage
, filmPrice
, filmReview
, and releaseDate
. These fields have different data types such as String
, Number
, and Date
, representing the respective properties of a film.
The optional second argument { collection: 'filmsCollection' }
specifies the name of the MongoDB collection where the film documents will be stored. In this case, the collection name is set to 'filmsCollection'
.
By default, Mongoose pluralizes the model name and converts it to lowercase to determine the collection name. In this case, if we removed the { collection: 'filmsCollection' }
option, Mongoose would assume that the collection name should be films
.
Once the schema is defined we can create a model:
The mongoose.model()
function is used to create a model based on the defined schema. A model acts as an interface for interacting with the MongoDB collection and performing database operations. In this case, the model is named 'Film'
, and it corresponds to the 'filmsCollection'
collection.
Finally export the Film model by adding:
Here we export the Film model so that it can be imported and used in other parts of the application.
In summary, this file sets up a Mongoose schema and model for a film document, specifying its fields and their data types. It also defines the collection name and exports the Film model for use in other modules.
Amend the connection db file as:
In order for our application to work with Mongoose we need to amend the connection to use mongoose.connect()
method. Amend the database file as follows:
The Mongoose connection does not need to be exported from this file. The connection can be established once in the application entry point, and other modules can access the connection using the mongoose package directly. In our case we need to edit index.js
.
Add the Db to the index
Ensure db is required in index.js
.
Edit the controller
Applying Mongoose to the controller logic we can now remove the need for the ObjectID code for getAllData()
and getDataById()
methods.
Amend the controller as follows: