0% found this document useful (0 votes)
23 views2 pages

Using Database Demo

The document demonstrates basic operations in MongoDB like creating a database and collection, inserting documents, creating indexes, aggregation operations like counting, grouping, sorting, etc. It creates an "orders" collection, inserts sample order documents, creates indexes on "C_id" and "item" fields, performs aggregation operations like counting total documents, grouping on customer id to find total order amount by status, finding distinct customer ids, and sorting in descending order of total order amount.

Uploaded by

devrepankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Using Database Demo

The document demonstrates basic operations in MongoDB like creating a database and collection, inserting documents, creating indexes, aggregation operations like counting, grouping, sorting, etc. It creates an "orders" collection, inserts sample order documents, creates indexes on "C_id" and "item" fields, performs aggregation operations like counting total documents, grouping on customer id to find total order amount by status, finding distinct customer ids, and sorting in descending order of total order amount.

Uploaded by

devrepankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

> show dbs

admin (empty)
local 0.078GB
USING DATABASE Demo
> use Demo
switched to db Demo

> show collections


CREATING COLLECTION ORDERS
> db.createCollection ("orders")
{ "ok" : 1 }

SHOW COLLECTIONS
> show collections
orders
system.indexes

INSRETING DATA IN COLLECTIONS


> db.orders.insert({"C_id":101,"item":"bags","amt":1000,"quantity":10,"status":"P"})
WriteResult({ "nInserted" : 1 })
> db.orders.insert({"C_id":102,"item":"mobile","amt":30000,"quantity":1,"status":"P"})
WriteResult({ "nInserted" : 1 })
> db.orders.find()
{ "_id" : ObjectId("53d3214c842d23951248d54d"), "C_id" : 101, "item" : "bags", "amt" : 1000, "quantity" : 0, "status"
: "P" }
{ "_id" : ObjectId("53d32192842d23951248d54e"), "C_id" : 102, "item" : "mobile",
"amt" : 30000, "quantity" : 1, "status" : "P" }
>
1) CREATING SIMPLE INDEX ON CUSTOMER ID AND ITEM ID
> db.orders.ensureIndex({"C_id":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.orders.ensureIndex({"item":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}

> db.orders.insert({"C_id":101,"item":"dress","amt":3000,"quantity":1,"status":"
P"})
WriteResult({ "nInserted" : 1 })
> db.orders.insert({"C_id":103,"item":"watch","amt":2000,"quantity":2,"status":"
N"})
WriteResult({ "nInserted" : 1 })
> db.order.find()
> db.orders.find()
{ "_id" : ObjectId("53d3214c842d23951248d54d"), "C_id" : 101, "item" : "bags", "
amt" : 1000, "quantity" : 10, "status" : "P" }
{ "_id" : ObjectId("53d32192842d23951248d54e"), "C_id" : 102, "item" : "mobile",
"amt" : 30000, "quantity" : 1, "status" : "P" }
{ "_id" : ObjectId("53d32468842d23951248d54f"), "C_id" : 101, "item" : "dress",
"amt" : 3000, "quantity" : 1, "status" : "P" }
{ "_id" : ObjectId("53d32486842d23951248d550"), "C_id" : 103, "item" : "watch",
"amt" : 2000, "quantity" : 2, "status" : "N" }
>

2) DISPLAY ALL CUSTOMER ID OF ORDER COLLECTION WITH NO


REPETATION
> db.orders.distinct("C_id")
[ 101, 102, 103 ]
>

3) TOTAL NUMBER OF ORDER RECIVED SO FAR.


> db.orders.count()
4
>

4) DISPLAY ALL CUSTUMER ID WITH THERE TOTAL ORDER AMOUNT


WHOSE ORDER IS DELIVERED.
> db.orders.aggregate([{"$match":{"status":"N"}},{"$group":{"_id":"C_id",Total:{
"$sum":"$amt"}}}])
{ "_id" : "C_id", "Total" : 2000 }
>

> db.orders.aggregate([{"$match":{"status":"N"}},{"$group":{"_id":"$C_id",Total:
{"$sum":"$amt"}}}])
{ "_id" : 103, "Total" : 2000 }
>

5)DISPLAY ALL CUSTUMER ID IN DESCREASING ORDER OF TOTAL


ORDER AMOUNT WHOSE ORDER ARE PENDING.
> db.orders.aggregate([{"$match":{"status":"P"}},{"$group":{"_id":"$C_id",Total:{"$sum":"$amt"}}},{$sort:{Total:-
1}}])
{ "_id" : 102, "Total" : 30000 }
{ "_id" : 101, "Total" : 4000 }
>

You might also like