Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Created January 11, 2024 12:47
Show Gist options
  • Save halitbatur/4cbcffefc44e4d2f73520dc9b7f52b8d to your computer and use it in GitHub Desktop.
Save halitbatur/4cbcffefc44e4d2f73520dc9b7f52b8d to your computer and use it in GitHub Desktop.
Mongoose and Express discussion
  1. What are the differences and connections between Mongoose schemas and models? How do they work together in an Express.js application?
  2. How does Mongoose handle data validation? Discuss the benefits of using Mongoose for data validation as opposed to doing it manually in your Express.js routes.
  3. What are virtuals in Mongoose? Discuss how and why you might use them in a project. Give examples of scenarios where virtuals can be particularly useful.
  4. What is population in Mongoose? How does it differ from simply storing object IDs in your documents? Discuss scenarios where population is beneficial and when it might be better to avoid it.
  5. How does Mongoose handle asynchronous operations? Discuss the role of promises and async/await in managing database interactions in an Express.js application.
@belalninja
Copy link

Room 1: Belal, Musab, Hassan, Mohammad Abdullah

1. What are the differences and connections between Mongoose schemas and models? How do they work together in an Express.js application?


Schemas

A Mongoose schema defines the structure of the document, default values, validators, etc., within a MongoDB collection. It's a blueprint for how your data should look. a schema answers "what will the data in this collection look like?"

Models

A Mongoose model is a wrapper on the Mongoose schema. A Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc. You can think of a model as a constructor which takes in plain JavaScript objects, then adds methods and hooks to them for interacting with the database. a model provides functionality like "Are there any records matching this query?" or "Add a new document to the collection"

2. How does Mongoose handle data validation? Discuss the benefits of using Mongoose for data validation as opposed to doing it manually in your Express.js routes.


  • Validation is defined in the SchemaType in mongoose. Validation is a middleware in mongoose.
  • Using Mongoose for validation we can define validation rules in the schema, ensuring consistency.
  • It simplifies route handlers by abstracting away the validation logic.
  • Mongoose validation reduces code duplication. Without Mongoose validation, we have to manually validate data every time before saving it, which can lead to a lot of duplicated code if you're saving data in multiple places in your application.
  • Error handling is connected by default with validation in mongoose. When a validation fails, Mongoose will return an error detailing what went wrong.

3. What are virtuals in Mongoose? Discuss how and why you might use them in a project. Give examples of scenarios where virtuals can be particularly useful.


In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents. They are typically used for computation based on the values of other fields. They are defined on the mongoose schema.
Use cases:

  1. to format the data in a way that's more convenient for the application. For example, combining firstName and lastName into fullName as shown above.
  2. for complex calculations. if you have a schema for a product with price and tax fields, you could use a virtual to calculate the total price.
  3. A common use case for virtuals is to create a virtual field for a password in a user schema. When the password is set, a hashing algorithm can be used to store a hashed version of the password in the database, while the plain text password remains as a virtual and never gets stored.

4. What is population in Mongoose? How does it differ from simply storing object IDs in your documents? Discuss scenarios where population is beneficial and when it might be better to avoid it.


Population in Mongoose is a feature that replaces ObjectIDs in a document with the actual data from the referenced documents, similar to a JOIN operation in SQL. This feature allows you to work with related data as if it was embedded directly into the document, simplifying your code and enhancing readability.

5. How does Mongoose handle asynchronous operations? Discuss the role of promises and async/await in managing database interactions in an Express.js application.


mongoose operations by default are asynchronous because they return promise whenever they get called. we can handle returned promise with .then() and .catch() or use async/await to make the code more concise.
Overall, promises and async/await are fundamental in managing database interactions in an Express.js application with MongoDB. using promises or async methods achieves a clean and manageable way to handle asynchronous operations, making it easier to work with the database and handle errors effectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment