Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Created January 2, 2024 13:28
Show Gist options
  • Save halitbatur/c199d5e2464032cf73e6be77903c997e to your computer and use it in GitHub Desktop.
Save halitbatur/c199d5e2464032cf73e6be77903c997e to your computer and use it in GitHub Desktop.
No SQl vs SQL discussion

Discuss the answers for these questions with you teammates and write your answers in the comments.

  1. What is the difference between SQL and NoSQL?
  2. What is referencing and what is embedding in MongoDB?
  3. Why should we embed more than referencing when we can in MongoDB?
  4. When should we prefer referencing over nesting in MongoDB?
  5. What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
  6. What is the difference between a table and a collection?

Some helpful resources:

@Karbejha
Copy link

Karbejha commented Jan 8, 2024

1- What is the difference between SQL and NoSQL?
SQL databases are relational, and NoSQL databases are non-relational. SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.

2-What is referencing and what is embedding in MongoDB?
Unlike embedded documents, referenced documents are stored in a separate collection to their parent document. Therefore, it's possible to retrieve the parent document without retrieving any of its referenced documents.

3-Why should we embed more than referencing when we can in MongoDB?
Embedded documents are an efficient and clean way to store related data, especially data that's regularly accessed together
choose to embed data instead of referencing it
Performance (Faster Reads/Reduced Latency/Updates)
a combination of embedding and referencing (hybrid approach) is sometimes used to strike a balance between performance and data organization.

4-When should we prefer referencing over nesting in MongoDB?
A document is frequently accessed but contains data that is rarely used.Embedding would only increase in-memory requirements. some situations where referencing might be a better choice than nesting

  • Many-to-Many Relationships
  • Frequently Updated Related Data
  • Large and Growing Datasets
  • Shared References
  • Optimized for Write Operations

5-What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
Object-relational-mapping is the idea of being able to write queries like the one above, as well as much more complicated ones, using the object-oriented paradigm of your preferred programming language. Long story short, we are trying to interact with our database using our language of choice instead of SQL.
SQL EX: SELECT * FROM users WHERE email = 'test@test.com';
ORM EX: var orm = require('generic-orm-libarry'); var user = orm("users").where({ email: 'test@test.com' });

6-What is the difference between a table and a collection?
Table(SQL) - RDBMS Maintains relations between the data�Fixed or predefined schema Data is stored in rows and columns Foreign Key relations are supported by DB. Data will not be stored if we violate any of the column data type or foreign key or primary key. Joins can be used effectively to query the data.�Vertically Scalable (would be limited on the hardware, say you cannot keep on adding RAM into a server machine, The machine has its own limit of how much RAM can be increased) Storing and Retrieving is comparatively slower when data is huge. MongoDB Collection - NoSQL DB
No relation is maintained between the data - Dynamic Schema Data is stored as Document Dynamic schema allows to save the document of any data type or any number of parameters. Horizontally Scalable which is simply can be done by adding more servers - Storing and Retrieving is faster No explicit foreign Key support is available whereas we can design the schema by having foreign key(but remember we need to maintain the relationship). $lookup performs similar operation like LEFT OUTER JOIN in SQL.

Team Members : 1- Mohamad Karbejha 2- Mohamad Sheikh Alshabab 3- Hammam Abu Shehadeh

@FarahArar
Copy link

Room 10: Farah ,Banan , Abdullah, Dana

1-SQL databases are relational, and NoSQL databases are non-relational. SQL databases use structured query language (SQL) and have a
predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are vertically scalable, while NoSQL
databases are horizontally scalable

2-Embedding involves nesting one document within another, it can simplify queries and improve read performance, especially for cases
where related data is always accessed together. It eliminates the need for additional queries and provides better performance.
Referencing involves storing references to other documents within a document, it allows for more flexible and scalable data management
as it avoids duplication of data. It also enables better performance for updating documents as changes in referenced documents don't
require updating every document that has a reference to them.

3-referencing and embedding are two different strategies for modeling relationships between documents.
Embedded documents are an efficient and clean way to store related data, especially data that’s regularly accessed together. In general,
when designing schemas for MongoDB, you should prefer embedding by default, and use references and application-side or database-side
joins only when they’re worthwhile.

4-The related data is small and won't change much.
There's a one-to-few or one-to-many relationship.
You want faster read performance, as you can retrieve all data in one query.

5-An ORM is simply a library that makes it easier to communicate with a database in code. It is basically a technique to query or perform
CRUD (Create, Read, Update, Delete) operations to the database, mainly RDBMS (Relational Databases), using an object-oriented paradigm.
With the help of ORM, you don`t actually need to use SQL at all. Why we use ORM:

Abstraction: ORM abstracts away the complexities of SQL queries and database interactions, allowing developers to work with objects in
their code instead of raw database tables.
Portability: ORM makes it easier to switch between different database systems since the underlying SQL is abstracted by the framework.
Productivity: ORM can reduce the amount of boilerplate code needed for database operations, making development faster and more
maintainable.

Assuming you have a table named 'users'
SELECT id, username, email FROM users WHERE age > 25; # Assuming you have a User class mapped to a 'users' table
from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
tablename = 'users'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
username = Column(String(50))
email = Column(String(50))
age = Column(Integer)

Creating an SQLalchemy engine and session

engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

Using ORM to query the database

users_over_25 = session.query(User.id, User.username, User.email).filter(User.age > 25).all()

6-A table and a collection are both used to organize and store data, the main difference is that a table is structured and schema-driven data
storage unit used in SQL databases, while a collection is a more flexible and schema-less data container used in NoSQL databases.

@MonaAlHajEid
Copy link

Team Members: Muna Al Haj Eid, Ahmed Mash, Mo'mena Salloum.

  1. They differ in :
    a. Data Model: SQL databases use a relational data model, which stores data in tables with rows and columns. NoSQL databases use a variety of data models, such as key-value, document, column-family, and graph.
    b. Scalability: SQL databases are vertically scalable, NoSQL databases, on the other hand, are horizontally scalable.
    c. Consistency: SQL databases use ACID transactions to ensure data consistency, NoSQL databases often use BASE transactions.
    d. Use Cases: SQL databases are well-suited for transactional systems, such as banking where data consistency is critical. NoSQL databases are well-suited for big data and real-time applications, such as social media where scalability and flexibility are more important.

  2. They are two ways to model relationships between documents.
    Referencing involves creating a separate collection for related data and storing the reference to that data in the document.
    Embedding involves storing related data within the same document.
    Referencing is useful when the related data is large or complex, or when the relationship between the data is many-to-many. Embedding is useful when the related data is small or simple, or when the relationship between the data is one-to-many.

  3. Embedding documents in MongoDB is an efficient and clean way to store related data, especially data that’s regularly accessed together. It allows to define schema in whatever way works for any given application, and nest documents inside each other to create optimal schemas.

  4. Referencing is useful when your data needs to be referred to from multiple locations. If data is only useful in relation to its parent document, embedding is the way to go.

  5. ORM stands for Object-Relational Mapping, it's simply a way to interact with a database like we do in SQL but instead using a programming language, we can also abstract away some of the complexities of interfacing with a database.
    Examples:
    Without ORM:
    SELECT * FROM users WHERE email = 'test@test.com';
    With ORM:
    var orm = require('generic-orm-libarry');
    var user = orm("users").where({ email: 'test@test.com' });

  6. Table: Typically associated with relational databases, organized in rows and columns.
    Collection: Often used in NoSQL databases, a container for diverse or unstructured data without a fixed schema.

@belalninja
Copy link

Room 7: Belal, Lina Abumahfood, Lujain Mansour, Noor Alrai

1. What is the difference between SQL and NoSQL?


SQL databases are relational, and NoSQL databases are non-relational. SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are vertically scalable, while NoSQL databases are horizontally scalable

2. What is referencing and what is embedding in MongoDB?


There are two different ways to model relationships between data within the documents.

Referencing involves storing a link (usually an ID) to another document in a separate collection.
This is similar to foreign keys in relational databases.

Embedding involves storing the related data directly within the parent document as a sub-document.
This essentially nests the child's document within the parent.

3. Why should we embed more than referencing when we can in MongoDB?


Embedding is more efficient and clean than referencing. referencing is a heavier process and makes queries more demanding. also, embedding is preferred when data are regularly accessed together. Embedding is suitable when the related data is frequently accessed together and the embedded data does not grow without limit.
The more often a given workload can retrieve a single document and have all the data it needs, the more consistently high-performance your application will be.

4. When should we prefer referencing over nesting in MongoDB?


Referencing should be used more sparingly as there are limitations to referenced documents and references are less efficient. if a given document needs to be accessed from several different places then it may make make sense to make it a reference rather than embedded. Moreover, referencing is useful when the related data is accessed independently or when the embedded data can grow significantly.

5. What are ORMs? Why do we use them? Give an example of an SQL request with and without using ORM.


ORMs are tools used in software development to make working with databases easier. They allow us to interact with databases using objects instead of writing raw SQL queries. They simplify database operations and make code more maintainable. Example without ORM: "SELECT * FROM users WHERE age > 25." Example with ORM: "users = User.query.filter(User.age > 25).all()."

6. What is the difference between a table and a collection?


  1. Table(SQL) - RDBMS
  • Maintains relations between the data
  • Fixed or predefined schema Data is stored in rows and columns
  • Foreign Key relations are supported by DB.
  • Data will not be stored if we violate any of the column data type or foreign key or primary key.
  • Joins can be used effectively to query the data.
  • Vertically Scalable (would be limited on the hardware, say you cannot keep on adding RAM into a server machine, The machine has its own limit of how much RAM can be increased) Storing and Retrieving is comparatively slower when data is huge.
  1. MongoDB Collection - NoSQL DB
  • No relation is maintained between the data - Dynamic Schema
  • Data is stored as Document
  • Dynamic schema allows to save the document of any data type or any number of parameters.
  • Horizontally Scalable which is simply can be done by adding more servers - Storing and Retrieving is faster
  • No explicit foreign Key support is available whereas we can design the schema by having foreign key(but remember we need to maintain the relationship

@Hayder000
Copy link

Team: Hayder, Baraa

A1: The main difference between SQL and NoSQL is that SQL is a relational DB, while NoSQL is a non-relational DB.

A2: Referenced documents are stored in a separate collection to their parent document, meanwhile embedding nests documents, simplifying queries

A3: Because embedded documents are efficient and good way to store related data, Referencing should be used more sparingly as there are limitations to referenced documents and references are less efficient.

A4: Referencing is preferred in MongoDB for reducing data duplication, handling frequent updates, and managing shared data efficiently.

A5: ORM is the concept of complicated writing queries using the OOP of whatever programming language you want use. We use it because we can write it in any programming language we're already using, plus it gives us support for transactions, connection pooling, and more. Below are some examples:
Without ORM: SELECT * FROM users WHERE username = 'Ahmad'
With ORM(JavaScript): let user = orm("users").where({ username: 'Ahmad' });

A6: Tables have a fixed schema and adding new columns requires modifying the entire table, collections can contain documents with different structures.

@MahmoudRumaneh
Copy link

Room 4 - Members: Farah Alsoqi, Mahmoud Rumaneh, Najwan Shawareb, Mohammad Abdullah

Q1:

SQL databases are characterized by their relational nature and fixed schema, making them suitable for applications that demand structured data, complex queries, and well-defined relationships. These databases scale vertically, prioritizing ACID properties to ensure transaction reliability.
On the other hand, NoSQL databases are non-relational, providing flexibility in data models with dynamic or schema-less structures. They excel in horizontal scalability, making them ideal for scenarios involving rapidly changing data, high scalability requirements, and situations where a flexible schema is paramount. It's worth noting that NoSQL databases may trade off some ACID properties for enhanced performance.
The choice between SQL and NoSQL hinges on the specific needs of the application, leading to the adoption of a polyglot persistence approach in many modern systems, where both types are utilized to cater to diverse data management requirements.

Q2:

Embedded documents are stored as children inside a parent document. This means they are all stored under one collection, and whenever you retrieve the parent document, you also retrieve all its embedded documents.

In MongoDB, referencing entails structuring data into distinct collections or documents and forming relationships through references or foreign keys. This method supports normalized data structures, prioritizing data consistency; however, it necessitates additional queries for accessing related data when navigating through the established references.

Q3:

In MongoDB, choose embedding for small to medium-sized, frequently accessed data, providing better performance and simplified schema. Choose referencing for large, frequently updated data, ensuring consistency across documents. The decision depends on your specific use case, and many applications use a combination of both approaches.

Q4:

Often preferred when dealing with large or frequently updated data, when query flexibility and consistency are critical.

Q5:

ORMs (Object-Relational Mappers) bridge object-oriented programming and databases, simplifying database interactions. They abstract SQL complexities, enhance productivity, offer portability, align with the object-oriented paradigm, and improve security.

Example SQL Request without ORM:

SQL Code:
SELECT * FROM users WHERE age > 25;
Example SQL Request with ORM (using SQLAlchemy in Python):

Q6:

A table, integral to relational databases (RDBMS), serves as a structured storage unit where data is arranged in rows and columns, adhering to a fixed schema. In contrast, a collection, found in NoSQL databases, offers a storage unit that allows for flexible and dynamic data modeling, devoid of a predefined schema. Examples of these structures include tables in RDBMS like MySQL and collections in NoSQL databases like MongoDB.

@peaceofcode1
Copy link

team members: jafar bino, Dana Maraqa , lin Daaboul, Ahmed shalash

Q1:
SQL databases are relational, and NoSQL databases are non-relational.
SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data.
SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.
SQL databases are table-based, while NoSQL databases are document, key-value, graph, or wide-column stores.
SQL databases are better for multi-row transactions, while NoSQL is better for unstructured data like documents or JSON.

Q2:
Referencing in MongoDB : is when we use a unique Identifier -to reference once document in another separate document, the reference is then used to $lookup the data when needed , usually used in frequently updated DB or when the data is considered large

Embedding in MongDB : mainly means that within the same document a reference is needed it could be done by adding a sub-document or document inside a parent document.
Embedding is usually used when the data doesn't change frequently and a document needs to be fetched on one go

Q3:
embedding allows for faster reads since all the relevant data is stored in one document. Referencing needs additional queries to get related data.

Q4
referencing should be preferred over nesting when the related data can grow large and lead to overly large documents that exceed the 16MB document size limit or subject to frequent updates also when the data needs to be accessed and managed independently of the parent doc

Q5:
ORMs (Object-Relational Mappers) it is a technique for converting data between database (which use tables and SQL queries) and other oop lang (like Python or Java).in simle words it connernt between these types of programing langueges.

why to use it ?

Simplicity: ORMs let you work with databases using code that's easier to understand, write, and maintain.

Avoidance of Raw SQL: Instead of writing SQL queries directly, you can use the programming language's syntax to interact with the database.
ex:
Fetch user by ID (Raw SQL):
String sqlQuery = "SELECT * FROM users WHERE id = 1;";
with ORM:

@entity
@table(name = "users")
public class User {
@id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String email;

}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {
public static void main(String[] args) {
// Hibernate setup
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(User.class)
.buildSessionFactory();

    Session session = factory.getCurrentSession();

    try {
        // Start a transaction
        session.beginTransaction();

        // Fetch user by ID using ORM
        User user = session.get(User.class, 1L); 
        // Commit the transaction
        session.getTransaction().commit();
        // Use the fetched user object
        System.out.println("Fetched user: " + user.getName());
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        factory.close();
    }
}

}

Q6:
Table (Relational Database):

Structure: A table is a structured set of data organized in rows and columns.
Schema: In relational databases, tables have a predefined schema, meaning the structure, data types, and constraints for each column are fixed and defined when creating the table.
Relationships: Relational databases emphasize relationships between tables. Data integrity is maintained using foreign key constraints to establish connections between different tables.
Collection (NoSQL Database):

Structure: A collection, in the context of NoSQL databases, is a container for storing data. It can store various forms of data structures such as documents, key-value pairs, or wide-column structures.
Schema: NoSQL databases often offer schema flexibility. Some variations allow collections to store different types of documents or entities with different structures within the same collection.
Relationships: While some NoSQL databases support relationships, they often handle relationships differently compared to relational databases. Some NoSQL databases emphasize denormalized data storage, where related data can be embedded within a single document or stored separately without explicit foreign keys.
Differences:

Schema Flexibility: Tables in relational databases have a fixed schema, whereas collections in NoSQL databases can have a flexible or even schema-less structure.
Data Structure: Tables consist of rows and columns, whereas collections in NoSQL databases can contain various forms of data structures based on the database type.
Relationship Handling: Relational databases use explicit relationships between tables (foreign keys), while NoSQL databases handle relationships differently, often by embedding related data within documents or using alternative mechanisms.

@fedabaqain
Copy link

Team : Wajd , Rinad, Feda
Q1: SQL : Structured Query Language, Organized into columns and rows within a table, SQL databases use a relational model that work best with well-defined structured data,SQL databases are scalable vertically, based on schema
NOSQL : non-relational databases , NoSQL databases are scalable horizontally , work well with unstructured data , NoSQL is schema-less (no fixed data model).

Q2: In referencing, relationships between documents are modeled by storing references (usually, the _id field) from one document to another. Referencing is useful when there is a one-to-many or many-to-many relationship between entities, and we want to avoid duplicating data.

In embedding, related data is stored directly within a document, eliminating the need for separate collections and references.
This is suitable for one-to-one or one-to-few relationships where the related data is small and doesn't change frequently.

Q3:Embedded documents are an efficient and clean way to store related data, especially data that’s regularly accessed together.
Embedded documents are stored as children inside a parent document. This means they are all stored under one collection, and whenever you retrieve the parent document, you also retrieve all its embedded documents.

Q4: If the related data is large or changes frequently, referencing can be more efficient , allow to perform , Referencing allows you to perform more complex queries that involve filtering, sorting, and aggregating related data independently. This flexibility can be crucial for certain reporting or analytical scenarios.

Q5-ORM stands for Object-Relational Mapping. It is a programming technique that converts data between incompatible type systems in object-oriented programming languages. In the context of databases, an ORM is a tool or library that simplifies database interactions by abstracting the database operations and allowing developers to work with objects in their programming language, rather than dealing directly with SQL queries. it help with code readability and productivity

Q6- Table (Relational Database):

In a relational database, data is organized into tables. A table consists of rows and columns, where each row represents a record, and each column represents a field or attribute. Tables are connected through relationships, and the structure of the data is defined by a fixed schema.

Collection (NoSQL Database):

In NoSQL databases, particularly document-oriented databases like MongoDB, data is organized into collections. A collection is a grouping of MongoDB documents. Each document is a set of key-value pairs, and collections are schema-less, meaning each document in a collection can have different fields. Collections in NoSQL databases are similar to tables but are more flexible and don't enforce a fixed schema.

@mohmmadms
Copy link

mohmmad smadi , nour kayyali , hakeema alzaidanin , ahmad juma

  1. 1-SQL databases are relational, and NoSQL databases are non-relational
    2-SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data
    3-SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.

  2. Embedding: Stores related data directly within a single document, nesting it as subdocuments.

Referencing : Stores a reference (usually the _id field) to a related document in a separate collection.
Similar to foreign keys in relational databases.

  1. Frequent read access ,Simple relationships, Limited data changes
    Reduced Number of Queries, Faster Access to Data

The typical rule is:

What you use together, store together.

  1. When we deal with complex data , such as many -to many relations and one-to-many relations , basically when we have a lot of joins

5)ORM (Object Relational Mapping) is a library that makes it easier to communicate with a database in code.
It is basically a technique to query or perform CRUD (Create, Read, Update, Delete) operations to the database, mainly RDBMS (Relational Databases), using an object-oriented paradigm. it makes CRUD operations faster.

ex: without using ORM :
const mysql = require("mysql");
const conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "you_password",
database: "mydb",
});

conn.connect(function (error) {
if (error) {
console.log(error);
} else {
console.log("Connected!");
let sql = "INSERT INTO Users (username, password) VALUES ('john-doe', 'randompassword')";
conn.query(sql, function (error, result) {
if (error) {
console.log(error);
} else {
console.log(result);
}
});
}
});

with using ORM:
const Sequelize = require("sequelize");
const sequelize = new Sequelize("mydb1", "your_username", "your_password", {
dialect: "mysql",
});

//Defining User model
const User = sequelize.define("User", {
username: Sequelize.STRING,
password: Sequelize.STRING,
});

sequelize.sync();

User.create({
username: "john-doe",
password: "randompassword",
}).then(function (user) {
console.log(user);
});

  1. We use tables with SQL , and collections with noSQL
    collections is a group of documents , a document is like a table in relational database

@Hassan-AbuGareeb
Copy link

team 6 Hassan AbuGareeb, Lunar Salameh, Sanad AlShobaki, Hadeel Obaid

1- SQL: (Structured Query Language) vs NOSQL(NOT only SQL) SQL databases are best used for structured data, NoSQL databases are suitable for structured, semi-structured, and unstructured data
-SQL databases are relational, and NoSQL databases are non-relational.
-noSql syntax is more flexible.
-SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.
-sql is table based, noSQL have 4 types (document, wide-column, graph,key-value).

2- Referencing involves storing references to related documents within a document,
Embedding involves nesting one document inside another.

3- to reduce dependency between collections, and localize data retrieval, reduce redundancy, increase query performance, increase simplicity and readability.

4- when there's Many-to-Many Relationships, Frequent Updates to Related Data, and when the amount of data and keys is large, referencing is preferred to reduce complexity.

5- ORM (Object–relational mapping): ORM provides a way to map between the objects in your code and the tables in a relational database, we use them to simplify syntax and makes interacting with databases in code easier,
EX:
no orm:
SELECT * FROM users WHERE username LIKE 'john%';

orm:
User.username.like('john%')

6- Table: The term "table" is typically associated with relational databases. In a relational database management system (RDBMS), data is organized into tables, each of which has a predefined schema with columns and data types.
Collection: The term "collection" is commonly used in the context of NoSQL databases, especially document-oriented databases like MongoDB. In MongoDB, data is stored in collections, and each document within a collection can have a different structure.

@sarajouma
Copy link

Sara jouma , Musab Sakhreyah , Malak Jabr

1-What is the difference between SQL and NoSQL?

  • SQL databases are relational, and NoSQL databases are non-relational.
  • SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data.
  • SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.
  • SQL databases are table-based, while NoSQL databases are document, key-value, graph, or wide-column stores.
  • SQL databases are better for multi-row transactions, while NoSQL is better for unstructured data like documents or JSON.

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.

  • 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 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?

  • Table(SQL) - RDBMS
  1. Maintains relations between the data
  2. Fixed or predefined schema Data is stored in rows and columns
  3. Foreign Key relations are supported by DB.
  4. Data will not be stored if we violate any of the column data type or foreign key or primary key.
  5. Joins can be used effectively to query the data.
  6. Vertically Scalable (would be limited on the hardware, say you cannot keep on adding RAM into a server machine, The machine has its own limit of how much RAM can be increased) Storing and Retrieving is comparatively slower when data is huge.
  • MongoDB Collection - NoSQL DB
  1. No relation is maintained between the data - Dynamic Schema
  2. Data is stored as Document
  3. Dynamic schema allows to save the document of any data type or any number of parameters.
  4. Horizontally Scalable which is simply can be done by adding more servers - Storing and Retrieving is faster
  5. No explicit foreign Key support is available whereas we can design the schema by having foreign key(but remember we need to maintain the relationship).

@gorgees04
Copy link

gorgees04 commented Jan 8, 2024

Gorgees , Ammar, Lubna

What is the difference between SQL and NoSQL?

  • SQL databases are table-based, while NoSQL databases can be document-oriented, key-value pairs, or graph structures.
  • SQL databases scale vertically, usually on a single server, and require users to increase physical hardware to increase their storage capacities. NoSQL databases offer horizontal scalability, meaning that more servers simply need to be added to increase their data load.
  • SQL databases use SQL (Structured Query Language). NoSQL databases use JSON (JavaScript Object Notation), XML, YAML, or binary schema, facilitating unstructured data.
  • SQL has a fixed-defined schema, while NoSQL databases are more flexible.

What is referencing and what is embedding in MongoDB?

  • Manual references save the _id field of one document in another document as a reference. Your application runs a second query to return the related data. These references are simple and sufficient for most use cases.
  • MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document.

Why should we embed more than referencing when we can in MongoDB?

Embedded documents are an efficient and clean way to store related data, especially data that’s regularly accessed together. In general, when designing schemas for MongoDB, you should prefer embedding by default, and use references and application-side or database-side joins only when they’re worthwhile. The more often a given workload can retrieve a single document and have all the data it needs, the more consistently high-performance your application will be.

When should we prefer referencing over nesting in MongoDB?

MongoDB applications use one of two methods to relate documents: Manual references save the _id field of one document in another document as a reference. Your application runs a second query to return the related data. These references are simple and sufficient for most use cases.
Referencing is how you acknowledge the source of the information you have used (referred to) in your work. It helps to make clear to the reader how you have used the work of others to develop your own ideas and arguments.

What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.

Object Relational Mapping a library that makes it easier to communicate with a database in code. It is basically a technique to query or perform CRUD (Create, Read, Update, Delete) operations to the database, mainly RDBMS (Relational Databases), using an object-oriented paradigm. With the help of ORM, you don"t actually need to use SQL at all. You can directly interact with the database and perform queries in the same language you are using for your back-end code

What is the difference between a table and a collection?

tables have a fixed schema and adding new columns requires modifying the entire table, collections can contain documents with different structures.

Example:

const mysql = require("mysql");
const conn = mysql.createConnection({
	host: "localhost",
	user: "your_username",
	password: "you_password",
	database: "mydb",
});

conn.connect(function (error) {
	if (error) {
		console.log(error);
	} else {
		console.log("Connected!");
    let sql = "INSERT INTO Users (username, password) VALUES ('john-doe', 'randompassword')";
    conn.query(sql, function (error, result) {
      if (error) {
        console.log(error);
      } else {
        console.log(result);
      }
    });
  }
});

Using Sequelize, the same code would become:


const Sequelize = require("sequelize");
const sequelize = new Sequelize("mydb1", "your_username", "your_password", {
	dialect: "mysql",
});

//Defining User model

const User = sequelize.define("User", {
	username: Sequelize.STRING,
	password: Sequelize.STRING,
});

sequelize.sync();

User.create({
	username: "john-doe",
	password: "randompassword",
}).then(function (user) {
	console.log(user);
});

@JanaAbuHaltam
Copy link

Jana AbuHaltam, Raneem AlHamarneh, Mohamad Alchehabi, Hala Qitouqa

Q1
SQL are relational databases and NoSQL are non-relational databases
SQL databases are vertically scalable, NoSQL databases are horizontally scalable
SQL are table-based, NoSQL could be key-value pairs, document-based, graph databases or wide-column stores
Examples of SQL databases: Oracle, MySQL
Examples of noSQL databases: MongoDB and CouchDB

Q2 Embedded Documents:

Used for one-to-many relationships with a "contains" or "has a" association.
Example: Blog posts containing multiple comments. All comments are embedded within the blog post document.
Pros: Better performance for read and update operations as everything is in a single document.
Cons: Risk of data duplication and limited by the maximum document size of 16MB.
References (Manual References):

Used for many-to-many relationships and complex hierarchical data.
Example: Blog posts and comments stored in separate collections; comments reference the blog post’s _id.
Pros: Avoids data duplication and is effective for complex relationships.
Cons: Slower read performance due to the need for multiple queries to join related data.

Q3 Referencing saves space and allows for more scalability, but it can be slower due to multiple queries and can be complex to maintain data consistency. Choosing between them depends on your specific needs regarding performance, data size, and complexity.

Q4 use referencing when dealing with large, complex, frequently changing, or interrelated datasets where embedding would be impractical or inefficient.

Q5 1-Simplifies Code
2-Enhances Maintainability
3-Database Abstraction
4-Reduces Boilerplate

Without ORM (Using Raw SQL in Node.js):

the code in JS

const mysql = require('mysql');

// Create connection
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'your_username',
    password: 'your_password',
    database: 'your_database'
});

// Connect to MySQL
connection.connect();

// Execute query
connection.query('SELECT * FROM users WHERE age > 30', (error, results, fields) => {
    if (error) throw error;
    // results is an array of records
    const users_over_30 = results;
    console.log(users_over_30);
});

// Close connection
connection.end();

With ORM (Using an ORM in Node.js):
const { Sequelize, Model, DataTypes } = require('sequelize');

// Connect to a database
const sequelize = new Sequelize('your_database', 'your_username', 'your_password', {
    host: 'localhost',
    dialect: 'mysql' // or 'sqlite', 'postgres', 'mssql'
});

class User extends Model {}

User.init({
    // Assuming a model structure, modify as needed
    age: DataTypes.INTEGER
}, { sequelize, modelName: 'user' });

async function getUsersOver30() {
    const users_over_30 = await User.findAll({
        where: {
            age: {
                [Sequelize.Op.gt]: 30
            }
        }
    });
    return users_over_30;
}

getUsersOver30().then(users => console.log(users));

Q6 table is structured collection of data organized in rows and columns
in collections, data is organized in documents

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