Creazione del database tramite MongoDB shell (mongosh):
use amazon_global_logisticsQuesto comando crea (se non esiste) e seleziona il database amazon_global_logistics, che contiene tutte le collezioni del progetto.
Creazione delle collezioni principali del sistema:
db.createCollection("assets")
db.createCollection("inventory")
db.createCollection("sellers")
db.createCollection("price_history")
db.createCollection("reviews")Le collezioni rappresentano le entità principali:
assets→ prodotti fisici/digitali/bundleinventory→ stock per venditoresellers→ venditori della piattaformaprice_history→ storico prezzi prodottireviews→ recensioni utenti
MongoDB non utilizza schemi rigidi, ma si definisce una struttura logica dei documenti.
{
_id: String,
name: String
}db.sellers.insertMany([
{ _id: "S1", name: "TechStore EU" },
{ _id: "S2", name: "Global Electronics" },
{ _id: "S3", name: "Digital World" }
]){
_id: String,
name: String,
type: String,
basePrice: Number
}db.assets.insertMany([
{ _id: "A1", name: "Gaming Monitor", type: "physical", basePrice: 300 },
{ _id: "A2", name: "Mechanical Keyboard", type: "physical", basePrice: 120 },
{ _id: "A3", name: "Office Suite License", type: "digital", basePrice: 80 },
{ _id: "A4", name: "Gaming Headset", type: "physical", basePrice: 150 },
{ _id: "A5", name: "Laptop Pro 15", type: "physical", basePrice: 1200 }
]){
sellerId: String,
assetId: String,
quantity: Number
}db.inventory.insertMany([
{ sellerId: "S1", assetId: "A1", quantity: 50 },
{ sellerId: "S1", assetId: "A2", quantity: 120 },
{ sellerId: "S1", assetId: "A3", quantity: 80 },
{ sellerId: "S2", assetId: "A1", quantity: 30 },
{ sellerId: "S2", assetId: "A4", quantity: 60 },
{ sellerId: "S3", assetId: "A5", quantity: 25 }
]){
assetId: String,
prices: [
{
price: Number,
date: Date
}
]
}db.price_history.insertMany([
{
assetId: "A1",
prices: [
{ price: 280, date: new Date("2024-03-01") },
{ price: 310, date: new Date("2024-03-10") }
]
},
{
assetId: "A2",
prices: [
{ price: 100, date: new Date("2024-03-01") },
{ price: 130, date: new Date("2024-03-10") }
]
}
]){
assetId: String,
userId: String,
rating: Number
}db.reviews.insertMany([
{ assetId: "A1", userId: "U1", rating: 5 },
{ assetId: "A1", userId: "U2", rating: 4 },
{ assetId: "A2", userId: "U3", rating: 5 },
{ assetId: "A3", userId: "U4", rating: 3 },
{ assetId: "A4", userId: "U5", rating: 4 }
])db.inventory.find(
{ sellerId: "S1" },
{ assetId: 1, quantity: 1, _id: 0 }
)Restituisce tutti i prodotti in stock per un venditore specifico.
db.price_history.aggregate([
{ $match: { assetId: "A1" } },
{ $unwind: "$prices" },
{
$group: {
_id: "$assetId",
avgPrice: { $avg: "$prices.price" }
}
}
])Calcola il prezzo medio storico del prodotto.
db.price_history.aggregate([
{ $match: { assetId: "A1" } },
{ $unwind: "$prices" },
{ $sort: { "prices.date": -1 } },
{ $limit: 1 }
])Restituisce l’ultimo prezzo inserito per un prodotto.
db.inventory.aggregate([
{ $match: { sellerId: "S1" } },
{
$lookup: {
from: "assets",
localField: "assetId",
foreignField: "_id",
as: "asset"
}
},
{ $unwind: "$asset" },
{
$group: {
_id: null,
totalValue: {
$sum: { $multiply: ["$quantity", "$asset.basePrice"] }
}
}
}
])Calcola il valore totale dello stock per un venditore.
db.reviews.aggregate([
{
$group: {
_id: "$assetId",
avgRating: { $avg: "$rating" }
}
}
])Restituisce la valutazione media per ogni prodotto.
Il backend è sviluppato in Node.js con Express e si occupa di:
- connessione a MongoDB
- gestione API REST
- calcolo stock e prezzi dinamici
- aggregazioni dati
- aggiornamento prezzi
node server.jsOutput atteso:
MongoDB connected
Server running http://localhost:3000
/amazon_project
│
├── server.js
├── index.html
├── package.json
└── /public (opzionale)
const client = new MongoClient("mongodb://admin:password@localhost:27017")
let db
async function start() {
await client.connect()
db = client.db("amazon_global_logistics")
console.log("MongoDB connected")
}
start()GET /api/sellersGET /api/seller-view/:sellerIdGET /api/assets-with-stockGET /api/reviews/:assetIdPOST /api/assets/:assetId/priceIl prezzo viene calcolato così:
- Se esiste
price_history→ usa ultimo prezzo aggiornato - Se non esiste → usa
basePrice
