Skip to content

Instantly share code, notes, and snippets.

@muslemomar
Created April 23, 2024 11:30
Show Gist options
  • Save muslemomar/09e1ca58664b52425c7820fcfc284956 to your computer and use it in GitHub Desktop.
Save muslemomar/09e1ca58664b52425c7820fcfc284956 to your computer and use it in GitHub Desktop.
  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.
@amolllat
Copy link

Amal Mohammed

  1. A Mongoose schema is a blueprint that defines the structure of documents within a MongoDB collection. It defines the shape of the documents, including the fields and their types. Essentially, it outlines what each document in the collection will look like.
    A Mongoose model is a Models are responsible for creating, reading, updating, and deleting documents in the MongoDB database. They encapsulate all the database operations related to a particular collection.
    use them in an Express.js application 🅰️ Define Schemas. b: Create Models. c: Use Models in Route Handlers. In the Express.js application, schemas and models work together to manage data persistence.

  2. Data Validation in Mongoose: Mongoose provides built-in support for data validation using schema validators and This helps ensure data integrity and consistency in the application. Benefits of using Mongoose for data validation include code organization, reusability, and centralized management of validation rules. By defining validation rules in the schema, Mongoose automatically validates data before saving it to the database.

3)Virtuals in Mongoose: are document properties that are not stored in the MongoDB database but are computed on the fly. Virtuals are useful for deriving new properties from existing ones, performing computations, or formatting data. This helps ensure data integrity and consistency in the application. For example, you might use virtuals to calculate a user's full name from separate first and last name fields or to format dates in a specific way.

4)Population in Mongoose allows you to reference documents in other collections and automatically retrieve their contents. It replaces the stored IDs with actual document objects when querying. The population is beneficial for denormalizing data and simplifying queries by avoiding multiple database lookups. However, excessive use of population can lead to performance issues, so it's important to use it judiciously. In scenarios where performance is critical or when dealing with large datasets, it might be better to avoid population and manually manage references using IDs.
5) Mongoose uses Promises to handle asynchronous operations by default, but it also supports callbacks and async/await syntax. When interacting with the database, Mongoose methods return Promises that can be chained or handled using async/await.

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