DEV Community

Avinash Maurya
Avinash Maurya

Posted on

NoSQL database

Certainly! Let's provide a short example for both a relational database (using SQL) and a non-relational database (using MongoDB, a document-oriented NoSQL database).

Relational Database (SQL - PostgreSQL):

Suppose we have two tables: users and orders. The following SQL query retrieves the names of users who made an order after a certain date.

SELECT users.name
FROM users
JOIN orders ON users.user_id = orders.user_id
WHERE orders.order_date > '2024-01-01';
Enter fullscreen mode Exit fullscreen mode

Non-Relational Database (MongoDB - NoSQL):

In MongoDB, suppose we have a collection called customers, and each document represents a customer with their orders as an embedded array. The following query retrieves customers who made a purchase after a certain date.

db.customers.find({
  "orders.orderDate": { $gt: ISODate("2024-01-01T00:00:00Z") }
}, {
  "name": 1,
  "_id": 0
});
Enter fullscreen mode Exit fullscreen mode

In this MongoDB example, we're using the $gt (greater than) operator to query based on the order date within the embedded array.

These examples illustrate the difference in query syntax and data modeling between relational and non-relational databases. In a relational database, data is often normalized into separate tables, and JOIN operations are used to combine related data. In a non-relational database, data may be denormalized and stored together in documents, with queries designed to navigate and filter within these documents.

Top comments (0)