- What is Mongoose?
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straightforward, schema-based solution to model your application data. Here’s an overview of Mongoose and its key features:
Key Features of Mongoose:
- Schema-Based Models:
- Mongoose allows you to define schemas for your data models, enforcing a structure on the documents within a MongoDB collection.
- Validation:
- Mongoose provides built-in validators as well as the ability to create custom validation rules for your schemas, ensuring that the data stored in your database is accurate and consistent.
- Middleware (Hooks):
- Mongoose supports middleware (also known as hooks) that can be executed before or after specific operations (e.g., save, remove, update). This allows you to run custom logic at various points in the lifecycle of a document.
- Type Casting:
- Mongoose automatically casts values to the specified data type in the schema, ensuring that the data saved to the database conforms to the expected types.
- Query Building:
- Mongoose provides a powerful API for building and executing queries against MongoDB, making it easier to interact with the database.
- Population:
- Mongoose has a feature called population, which allows you to reference documents in other collections and automatically replace the referenced IDs with the actual documents.
- Instance Methods and Static Methods:
- You can define custom instance methods and static methods on your Mongoose models to encapsulate logic that is specific to your application.
- Plugins:
- Mongoose supports plugins, which allow you to encapsulate reusable logic and share it across different models.
- Downloading the Project from Github
Create a folder named ‘MongoDB’ and download the source code from the provided link.
https://github.com/oguzhanpeker/mongodb.git
- Installing the Node Modules
To install the necessary Node.js modules, open the terminal in VS Code and execute the command provided.
npm install

- Running the Application
Open the ‘config.env’ file and update the ‘DATABASE’ and ‘DATABASE_PASSWORD’ variables with your MongoDB credentials. If you’re unsure how to set up a MongoDB cluster in the cloud, you can refer to this page and follow the instructions provided.

To initiate the application, run the given command in the terminal.
npm run start

- Importing Development Data
Open a new terminal in VS Code and execute the following command.
node dev-data/data/import-dev-data.js --import
The data from the ‘tours-simple.json’ file will be imported into the MongoDB database.

The verification can be done using the Postman application.
Execute the following URL with GET method on Postman to retrieve all tours.
127.0.0.1:3000/api/v1/tours
The request should retrieve the imported data from the database.

Additionally, we can access the MongoDB cloud and log in with our account to check whether the data has been imported.

NOTE: Creating a new tour, updating an existing one, or deleting a tour can all be tested using Postman. Resources on how to conduct these tests with Postman are available here.
- Filtering
Data can be filtered from the database, for instance:
- Duration is greater than or equal to 5
- Difficulty is set to ‘easy’
- Price is less than 1500
To apply these filters, execute the corresponding URL using the GET method in Postman.
127.0.0.1:3000/api/v1/tours?duration[gte]=5&difficulty=easy&price[lt]=1500
You will get a response with two tours only which are matching with the filter applied.

- Sorting
We can sort the data returned by the API using the following URL.
127.0.0.1:3000/api/v1/tours?sort=price
This will display tours ranging from the minimum to the maximum price.

To display tours from maximum to minimum, you may utilize the specified URL.
127.0.0.1:3000/api/v1/tours?sort=-price