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.
@fedabaqain
Copy link

Team: Fda, Noor, Momena
Q1: A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records,

Q2-a customizable middleware that gets defined inside the Schema Type of Mongoose schema. It automatically fires off before a document is saved in the NoSQL DB , Type of validation ( Built-in validation ,Custom validation), the benefit is that it has a built in validation and Error Handling the cabiblity of defining Default Values which lead to more code organization.

Q3- is feature that allow you to define a property on document that is not stored in MongoDB . Virtuals are typically used for computed properties on documents. , let's say we have a User model. Every user has an email, but you also want the email's domain. For example, the domain portion of '[[email protected]]' is 'gmail.com'. Below is one way to implement the domain property using a virtual. You define virtuals on a schema using the [Schema#virtual() function]

Q4-Population is the process or method of automatically replacing the specified paths in the document with document(s) from other collection(s).is a method used to automatically replace the _id or id fields of a document with the a document from another collection. when can use to a void data duplication so we can use reference to another document to have consistency in data so updates apply on places . avoid population when we have Frequency of Updates to avoid heavy database operation and effect on performance

Q4- part 2 . store only the object ID, don't have access to the actual data of the referenced document. if you want to display the data of the referenced document, you'll need to perform an additional query to retrieve it.
Populating the referenced document, allows you to automatically replace the object ID with the actual data of the referenced document.
This means that you can retrieve all the data you need in a single query.

Q5-
Mongoose uses promises to handle asynchronous operations. Promises are a way to handle asynchronous operations in JavaScript, and they provide a way to handle errors and return values in a consistent and predictable way. using .then() and .catch() methods , Mongoose functions that return Promises can be used with async/await, providing a more synchronous code structure.

@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