Project 2
1. Design your ecommerce database in MongoDB with the specifications provided Below :
- Every Customer would register on the ecommerce website with all their personal information
- Each Customer has a type of being normal or premium member
- The customer should be able to view products and add them to cart
- Products can belong to multiple categories, brands and segments.
- Each category can have subcategories as well.
- Each products can also have multiple tags associated with it.\
- From the cart, the customer should be able to check out and make payment and his/her order would be booked which the customer can track
- The admin of the ecommerce and the seller both can add products under multiple categories, segments and tags
- The seller would receive the orders and should be able to fulfil them.
- The order shipping details and the billing information are very important things to be stored in the database as well.
- Every Seller would register on the ecommerce website with all their personal information.
- Create your collections for your ecommerce database in MongoDB and fill it with sample information
Introduction
MongoDB is a Nosql database collection that focuses on availability and scalability. The data is structured and stored in collections of JSON documents.
By not using a fixed data structure, MongoDB provides a comprehensive solution for high-volume storage in a modern distributed system.
example command:-
test> use sample
switched to db sample
sample> show dbs;
admin 40.00 KiB
config 36.00 KiB
local 88.00 KiB
mongoworkshop3 40.00 KiB
shop 40.00 KiB
test 208.00 KiB
sample> db.test.insert({"name":"siva"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId("63819bd7391640af6134189e") }
}
sample>db.sample.find()
>db.createcollction customer
>show collection
>db.customer.insert({"customer":1,"name":"surya"});
>shoe collection
>db.createcollection.drop()
>db.customer.find()
2.Submit all the queries used to create the database
Answers:-
a. db.customer.insert( {customerID: 24,firstname:"john",lastname:"H",addresses:[{street:"23 peter st"}]);
b. db.customer.insert({normal memer:"3",premium member:"1"})
c.db.carts.insert({ _id: "product_id", status:'active', quantity: 2, total: 2000, products: []});
d.db.products.insertMany([{"categories":2,"brands":"new","segments":1}]);
e.db.category.insertOne([{"subcategories":5}]);
f. db.aggregateproducts.insertMany([{tags:['categories','subcategories'],like:50}]);
g. db.createcollection "(cart")
db.carts.insert({ _id: "product_id", status:'active', login: 2, logout: 0, products: []});
db.tracking:( { tracking_number: "22122X211SD", status: "on",estimated_delivery: new ISODate("2012-05-17T08:14:15.656Z") },
h. db.createcollection("admin")
db.admin.inserts({"sellerid":56})
db.product.insertMany([{'categories'],like:100}]);
i. db.createcollection("seller")
db.orders.insert({ created_on: new ISODate("2012-05-17T08:14:15.656Z")});
j.db.createcollection("order")
tracking:( { tracking_number: "22122X211SD", status: "cancelled"});
payment: { method: "visa", transaction_id: "2312213312XXXTD" }
k. db.seller.insertOne({"phone_no":3458927628,"name":"sugu","address":"4,old road"});
Simple Queries to perform on ecommerce db
1.Write a query to find the average order value for all Customers.
db.collectionname.find().aggregate(field_name).sort()
db.customer.find().sort{name:1}).limit(5)
db.customer.find().limit(5).sort({name:1})
2.Query your database to get top 10 customers whose orders have the highest value and who have been customers since less than 2 months
db.customer.find({months:{$lt:10}};
db.checkdate.find( { status: "delivery",estimated_delivery: new ISODate("2012-05-17T08:14:15.656Z") });
3.Query your database to find the sum of all orders received in this month
db.orders.find({$groupby:[{$max:"orders"},{"customer_date"<60}]});
4.Query your database to find customers count in various cities, group them by names of cities
db.aggregatecustomers.insertMany([{titles:'cities',description:'names',by:'count',tags:['cities','names','count'],likes:40}]);
5.Query your database to get all the sellers who have base in the same city in which the customers who ordered their products. Order them in the order of highest number of products sold to customers in same city to lowest.
db.customers.aggregate([{$group:[_id:{products:"$products"},count:{$sum:1}}},{$match:{count:{"$lt":1}}}]);