mongodb query exercise on order customer
mongodb query exercise on order customer
Let's
assume we have the following collections:
```json
"_id": 1,
"name": "Alice",
"email": "[email protected]"
},
"_id": 2,
"name": "Bob",
"email": "[email protected]"
},
"_id": 3,
"name": "Charlie",
"email": "[email protected]"
```
```json
[
"_id": 101,
"customer_id": 1,
"product": "Laptop",
"amount": 1200
},
"_id": 102,
"customer_id": 2,
"product": "Phone",
"amount": 800
},
"_id": 103,
"customer_id": 1,
"product": "Tablet",
"amount": 300
```
#### 1. Find all orders made by a specific customer (e.g., customer with `_id` 1):
```javascript
db.orders.find({ customer_id: 1 })
```
#### 2. Find customer information along with their orders using an aggregation pipeline:
```javascript
db.customers.aggregate([
$lookup: {
from: "orders",
localField: "_id",
foreignField: "customer_id",
as: "orders"
])
```
#### 3. Find customers who have spent more than $1000 in total:
```javascript
db.orders.aggregate([
$group: {
_id: "$customer_id",
},
$match: {
},
$lookup: {
from: "customers",
localField: "_id",
foreignField: "_id",
as: "customer"
},
$unwind: "$customer"
},
$project: {
_id: 0,
name: "$customer.name",
email: "$customer.email",
totalSpent: 1
}
}
])
```
```javascript
db.orders.aggregate([
$group: {
_id: "$customer_id",
orderCount: { $sum: 1 }
},
$lookup: {
from: "customers",
localField: "_id",
foreignField: "_id",
as: "customer"
},
$unwind: "$customer"
},
{
$project: {
_id: 0,
name: "$customer.name",
email: "$customer.email",
orderCount: 1
])
```
#### 5. Find all customers who have not placed any orders:
```javascript
db.customers.aggregate([
$lookup: {
from: "orders",
localField: "_id",
foreignField: "customer_id",
as: "orders"
},
$match: {
orders: { $eq: [] }
}
},
$project: {
_id: 0,
name: 1,
email: 1
])
```
These queries demonstrate how to interact with `customer` and `order` collections in MongoDB,
performing lookups, aggregations, and filtering to obtain meaningful information from the data.
Sure! Here's an exercise involving MongoDB queries. Let's assume you have the following two
collections: `customers` and `orders`.
```json
```
### Orders Collection
```json
```
### Solutions
```javascript
const alice = db.customers.findOne({ name: "Alice" });
```
```javascript
db.orders.aggregate([
$group: {
_id: "$customer_id",
},
$lookup: {
from: "customers",
localField: "_id",
foreignField: "_id",
as: "customer"
},
$unwind: "$customer"
},
{
$project: {
_id: 0,
name: "$customer.name",
email: "$customer.email",
totalSpent: 1
]);
```
#### 3. List all customers who have placed at least one order:
```javascript
db.orders.distinct("customer_id").forEach(customer_id => {
});
```
```javascript
db.orders.aggregate([
$sort: { date: -1 }
},
$group: {
_id: "$customer_id",
},
$lookup: {
from: "customers",
localField: "_id",
foreignField: "_id",
as: "customer"
},
$unwind: "$customer"
},
$project: {
_id: 0,
name: "$customer.name",
email: "$customer.email",
mostRecentOrder: 1
]);
```
#### 5. Identify customers who have spent more than $1000:
```javascript
db.orders.aggregate([
$group: {
_id: "$customer_id",
},
$match: {
},
$lookup: {
from: "customers",
localField: "_id",
foreignField: "_id",
as: "customer"
},
$unwind: "$customer"
},
$project: {
_id: 0,
name: "$customer.name",
email: "$customer.email",
totalSpent: 1
]);
```
#### 6. List all customers and include their orders (use aggregation):
```javascript
db.customers.aggregate([
$lookup: {
from: "orders",
localField: "_id",
foreignField: "customer_id",
as: "orders"
]);
```
#### 7. Find all customers who have not placed any orders:
```javascript
db.customers.aggregate([
$lookup: {
from: "orders",
localField: "_id",
foreignField: "customer_id",
as: "orders"
},
$match: {
orders: { $eq: [] }
},
$project: {
_id: 0,
name: 1,
email: 1
]);
```
These queries cover a range of operations, including basic retrieval, aggregation, and data joining. They
should provide a good exercise in working with MongoDB queries.