Managing data in MongoDB databases can be challenging, especially when dealing with complex data models. MongoDB is a schema-less database, which means that data can be added without following any particular structure. This flexibility makes MongoDB ideal for storing large amounts of data but also makes managing the data more difficult.

Mongoose provides a schema-based solution that helps ensure that data stored in MongoDB is consistent and formatted correctly. With Mongoose, you can define a schema for your data model, which specifies the structure of the data and the rules for how that data should be formatted. Here you will learn how to use mongoose in an express application.

Setting up your development environment

Before using Mongoose, you need to install it as a dependency in your project.

After installing Mongoose in your project, you need to connect your application to MongoDB using Mongoose.

Mongoose connects to a MongoDB database using the connect method, which takes a MongoDB URI as an argument.

The above code block connects to a local MongoDB instance and logs a success message when your application successfully connects to MongoDB.

Mongoose model making

The Mongoose model is a schema-based class in Mongoose that allows you to interact with MongoDB collections.

The Mongoose schema defines the structure of the documents you can store in a MongoDB collection and provides an interface for creating, reading, updating, and deleting documents in that collection.

When you define a Mongoose model, you define the schema for the documents in that collection, including the properties, their types, and any validations.

The above code block defines a Mongo schema with three properties: name, email, and age. Each property has a defined set of rules that you have to follow while mapping a value to its specified field.

The name property is a string type that is marked as required, which means you must map a string to this field. If you leave the field blank or enter a different JavaScript data type, Mongoose throws an error.

The email property marked as required is a string type. It doesn’t have any other validation rules, but in practice, you should verify whether the email is correct. You can validate email using regex expressions or a third-party library, such as class-validator.

The Age property is a Number type with a custom validation rule that checks whether the value mapped to the field is greater than zero. If the value does not pass validation, Mongoose throws an error with the message Please enter a valid age. You can leave this field blank as it is not marked as required.

After defining the schema, the code block creates a Mongoose model called User using the Mongoose.model() method. This method takes two arguments: the name of the model and the schema to be used for the document.

Finally, the User model is exported to other parts of your application.

Interacting with MongoDB using Mongoose

Your application is connected to Mongoose database and your model is created and accessible to other parts of your application, you can interact with your database using methods provided by Mongoose.

For this tutorial, you will be performing CRUD operations on the MongoDB database.

Leave a Reply

Your email address will not be published. Required fields are marked *