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

0Rawan commented Apr 23, 2024

Dawood AlKawaz, Teba Kaad, Elaf Gardi and Rawan Mustafa

  1. mongoose use async/await to execute queries accordingly and avoiding callback hell
    which also allow us to wait for response and display the predictable result
  2. Mongoose using the schema options provided like data types and other properties like unique, required or default to make sure that data are valid before saving the document
  3. Mongoose domains are not used for interaction with database directly instead it works by defining them and taking a specific value from the document to run a callback upon and return a result.
    This could be handy for example on an e-commerce website where you can get all the products in specific order to calculate the net price
  4. Mongoose population is a way to simply refer to documents on other collections, so when we add an ObjectId for example it will check the referenced collection if it has that document and not taking our word for granted
    It will consume complicated queries which will be a drawdown regarding the run time to query documents on multiple collections
  5. Mongoose use async/await to execute queries accordingly and avoiding callback hell
    which also allow us to wait for response and display the predictable result

@mawjaljadirjy
Copy link

1.

  • In Mongoose, a schema defines the structure of documents in a MongoDB collection, specifying fields, types, validators, etc.
  • A model is a compiled version of a schema, used to interact with MongoDB collections by creating, reading, updating, and deleting documents.
  • In an Express.js application, you define schemas using mongoose.Schema() and create models with mongoose.model().
  • Models are used within Express.js route handlers to perform CRUD operations on the database, based on the defined schemas.

2.

  • Mongoose handles data validation through schema-level validation rules defined using built-in and custom validators.
  • Built-in validators include required, min, max, enum, match, etc.
  • Custom validation functions can also be defined to enforce complex validation logic.
  • Mongoose automatically validates data against the schema rules when creating or updating documents, throwing validation errors if any rules are violated.
  • Benefits of using Mongoose for data validation include centralized logic, declarative syntax, automatic error handling, and integration with middleware, compared to manual validation in Express.js routes.

3.

  • Virtuals in Mongoose are computed properties defined on schemas that are not stored in the database.
  • They are defined using getters and setters and can be accessed like regular fields on documents.
  • Virtuals are useful for deriving properties, formatting data, performing aggregations, and enhancing data security in a project.
  • Examples include computing fullName, formatting dates, performing aggregate calculations, and maintaining data privacy.

4.

  • In Mongoose, population refers to automatically replacing specified paths in a document with actual documents from other collections by storing their ObjectIds.
  • It simplifies data retrieval, maintains data integrity, and reduces data duplication compared to simply storing ObjectIds.
  • Population is beneficial for managing one-to-many relationships, maintaining data integrity, and reducing data duplication.
  • However, it may introduce performance overhead, especially for high cardinality relationships, and may not be suitable for scenarios where embedded data is more efficient or performance-sensitive.

5.

  • Mongoose handles asynchronous operations using promises, which represent the eventual completion or failure of database operations.
  • Promises enable chaining of asynchronous operations and error handling using .then() and .catch().
  • Async/await syntax in Mongoose allows developers to write asynchronous code in a synchronous-looking manner, improving readability and maintainability.
  • In Express.js applications, promises or async/await can be used to manage database interactions within route handlers or middleware functions.
  • Benefits include readable code, simplified error handling, and sequential execution of asynchronous operations.

@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