- What is the difference between SQL and NoSQL?
- What is referencing and what is embedding in MongoDB?
- Why should we embed more than referencing when we can in MongoDB?
- When should we prefer referencing over nesting in MongoDB?
- What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
- What is the difference between a table and a collection?
Some helpful resources:
Sara jouma , Musab Sakhreyah , Malak Jabr
1-What is the difference between SQL and NoSQL?
2-What is referencing and what is embedding in MongoDB?
Embedding: Stores related data directly within a single document as nested subdocuments. All data is kept together in one collection.
Referencing: Involves storing the identifier (usually the _id field) of one document within another document. Think of it like a foreign key in a relational database. Documents are stored in separate collections.
3-Why should we embed more than referencing when we can in MongoDB?
Performance:
Faster reads: Retrieving related data is significantly faster with embedding since all the information is in one document, requiring only one query. This reduces roundtrip traffic between the application and database, boosting responsiveness.
Reduced complexity for join operations: Joins are typically computationally expensive, especially for complex relationships. Embedding eliminates the need for joins entirely when accessing frequently used related data, improving query performance.
Data consistency:
Atomic updates: You can update the parent document and its embedded subdocuments in a single atomic operation. This ensures data consistency and avoids potential synchronization issues that can arise with separate collections and referencing.
Simplicity and maintainability:
Easier schema design: Embedding can keep your schema simpler, especially for one-to-one or one-to-few relationships. This reduces complexity and makes it easier to understand and maintain.
Reduced code complexity: Your application code can be simpler as you don't need to perform explicit joins or handle references and foreign keys.
4-When should we prefer referencing over nesting in MongoDB?
Complex Relationships:
Many-to-many relationships: When a document can belong to multiple collections or vice versa, referencing allows better representation and flexibility. Nesting would lead to duplicated data and complex structures.
Independent updates and queries: If you frequently need to update or query the related data independently of the parent document, referencing allows separation of concerns and avoids unnecessary updates to the entire nested structure.
Data Management:
Large data sets: Nesting large subdocuments within parent documents can significantly increase document size, impacting performance and potentially exceeding document size limits. Referencing keeps documents smaller and more manageable.
Frequently changing data: If the related data changes frequently, embedding it can lead to unnecessary updates of the entire document, affecting performance and consistency. Referencing allows independent updates, improving maintenance and efficiency.
Flexibility and Scalability:
Data reusability: If the same related data is used by multiple documents, referencing avoids duplication and allows efficient data reuse across the schema. Nesting would lead to redundant storage and potential inconsistencies.
Evolving schema: Referencing provides greater flexibility for future schema changes, especially as relationships evolve or new entities are added. Nesting can become rigid and limit adaptability.
5-What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
ORM stands for Object-Relational Mapping, which is a technique that allows developers to interact with a database using an object-oriented paradigm. ORMs abstract away the complexity of interfacing with a database and allow developers to write code in the language they are already using. ORMs can speed up development time, decrease the cost of development, handle the logic required to interact with databases, and improve security by eliminating the possibility of SQL injection attacks. However, learning how to use ORM tools can be time-consuming, and they may not perform better weps documents smaller and more manageable.
Frequently changing data: If the related data changes frequently, embedding it can lead to unnecessary updates of the entire document, affecting performance and consistency. Referencing allows independent updates, improving maintenance and efficiency.
Data reusability: If the same related data is used by multiple documents, referencing avoids duplication and allows efficient data reuse across the schema. Nesting would lead to redundant storage and potential inconsistencies.
Evolving schema: Referencing provides greater flexibility for future schema changes, especially as relationships evolve or new entities are added. Nesting can become rigid and limit adaptability.
5-What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
ORM stands for Object-Relational Mapping, which is a technique that allows developers to interact with a database using an object-oriented paradigm. ORMs abstract away the complexity of interfacing with a database and allow developers to write code in the language they are already using. ORMs can speed up development time, decrease the cost of development, handle the logic required to interact with databases, and improve security by eliminating the possibility of SQL injection attacks. However, learning how to use ORM tools can be time-consuming, and they may not perform better when very complex queries are involved.
Example of an SQL request to retrieve information about a particular user:
SELECT * FROM users WHERE id = 123;
Example of the same request using an ORM:
User.find(123)hen very complex queries are involved.
Example of an SQL request to retrieve information about a particular user:
SELECT * FROM users WHERE id = 123;
Example of the same request using an ORM:
User.find(123)
6-What is the difference between a table and a collection?