Project 2
1 & 2)
MONGODB
use eCommerce
db.products.insert({
sku: "111445GB3",
title: "Simsong One mobile phone",
description: "The greatest Android phone on the market .....",
manufacture_details: {
model_number: "A123X",
release_date: new ISODate("2012-05-17T08:14:15.656Z")
},
shipping_details: {
weight: 350,
width: 10,
height: 10,
depth: 1
},
quantity: 99,
pricing: {
price: 1000
}
})
db.products.update({sku: "111445GB3"}, {$set: { categories: ['mobile/15G', 'mobile/fm'] }});
db.products.ensureIndex({categories:1 })
db.products.find({categories: /^mobile\/fm/})
{
title: "Mobiles containing a FM radio",
parent: "mobile",
path: "mobile/fm"
}
db.categories.insert({title: "Mobiles containing a FM radio", parent: "mobile", path: "mobile/fm"})
db.categories.insert({title: "Mobiles with 15G support", parent: "mobile", path: "mobile/15G"})
db.categories.ensureIndex({parent: 1, path: 1})
db.categories.ensureIndex({path: 1})
db.categories.find({parent: /^mobile/}, {_id: 0, path: 1})
{"path": "mobile/fm"}
{"path": "mobile/15G"}
db.carts.insert({
_id: "the_users_session_id",
status:'active',
quantity: 2,
total: 2000,
products: []});
db.carts.update({
_id: "the_users_session_id", status:'active'
}, {
$set: { modified_on: ISODate() },
$push: {
products: {
sku: "111445GB3", quantity: 1, title: "Simsong One mobile phone", price:1000
}
}
});
db.products.update({
sku: "111445GB3", quantity: {$gte: 1}
}, {
$inc: {quantity: -1},
$push: {
in_carts: {
quantity:1, id: "the_users_session_id", timestamp: new ISODate()
}
}
})
db.carts.update({
_id: "the_users_session_id", "products.sku": "111445GB3", status: "active"
}, {
$set: {
modified_on: new ISODate(),
"products.$.qty": new_quantity
}
})
db.products.update({
sku: "111445GB3",
"in_carts.id": "the_users_session_id",
quantity: {
$gte: 1
}
}, {
$inc: { quantity: (-1)*quantity_delta },
$set: {
"in_carts.$.quantity": new_quantity, timestamp: new ISODate()
}
})
db.products.update({
sku: product.sku,
"in_carts.id": cart._id,
"in_carts.quantity": product.quantity
}, {
$inc: {quantity: item.quantity},
$pull: {in_carts: {id: cart._id}}
})
}
db.carts.update({
_id: cart._id,
$set: {status: 'expired'}
})
}
db.orders.insert({
created_on: new ISODate("2012-05-17T08:14:15.656Z"),
shipping: {
customer: "Peter P Peterson",
address: "Longroad 1343",
city: "Peterburg",
region: "",
state: "PE",
country: "Peteonia",
delivery_notes: "Leave at the gate",
tracking: {
company: "ups",
tracking_number: "22122X211SD",
status: "ontruck",
estimated_delivery: new ISODate("2012-05-17T08:14:15.656Z")
},
},
payment: {
method: "visa",
transaction_id: "2312213312XXXTD"
}
products: {
{quantity: 2, sku:"111445GB3", title: "Simsong mobile phone", unit_cost:1000, currency:"USDA"}
}
})
db.carts.update({
_id: "the_users_session_id"
}, {
$set: {status:"complete"}
});
db.products.update({
"in_carts.id": "the_users_session_id"
}, {
$pull: {in_carts: {id: "the_users_session_id"}}
}, false, true);
-----------------------------------------------------------------------------------------------------------------------------
Simple Queries to perform on ecommerce db
1)
db.getCollection('order').aggregate([{
$lookup:
{
from: 'products',
localfield: 'product_id',
foreignField: 'id' ,
as: 'orderdetails',
}
}]);
db.bios.find().limit( 5 )
db.bios.find().sort({name:1 }).limit( 5)
db.bios.find().limit( 5).sort({ name: 1 })
2)
db.orders.find( { $and: [$max:"orders"},{"customer_date"<60}]})
3)
db.orders.aggregate([
{
$group:
{
id: { day: { $dayOfMonth: "$date"}, month:{$month:"$date"}},
totalAmount: {$sum: "$qty"},
count: {$sum: 1}
}
}
]) ;
4)
db.customers.aggregate([
{
$group:
{
id: null, count:{$sum:"$city"},
count: {$sum:1}
}
}
]);
db.customers.count({customer_city :{$sum: city})