You are on page 1of 4

Name : Shrihari Prasad Kalaskar

Roll no: 35

Batch: T2

ASSIGNMENT: 11
Problem Statement: Implement aggregation and indexing with suitable example using mongoDB.

PROGRAM:
student@student-OptiPlex-3010:~$ mongo
MongoDB shell version: 3.2.19
connecting to: test
> use mamta
switched to db mamta

> db.createCollection("mahie")
{ "ok" : 1 }
> db.mahie.insert([{title: 'mongodb overview',description: 'MongoDB is no sql database',by_user:'xyz',likes:10}])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 1,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
> db.mahie.insert([{title: 'nosql overview',description: 'no sql is very fast',by_user:'xyz',likes:100}])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 1,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
> db.mahie.insert([{title: 'indexing overview',description: 'index helps in search queries',by_user:'pqr',likes:10}])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 1,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
> db.mahie.find().pretty()
{
    "_id" : ObjectId("5ba4c20b7f8b526e5ec46013"),
    "title" : "mongodb overview",
    "description" : "MongoDB is no sql database",
    "by_user" : "xyz",
    "likes" : 10
}
{
    "_id" : ObjectId("5ba4c2687f8b526e5ec46014"),
    "title" : "nosql overview",
    "description" : "no sql is very fast",
    "by_user" : "xyz",
    "likes" : 100
}
{
    "_id" : ObjectId("5ba4c2c27f8b526e5ec46015"),
    "title" : "indexing overview",
    "description" : "index helps in search queries",
    "by_user" : "pqr",
    "likes" : 10
}
> db.mahie.aggregate()
{ "_id" : ObjectId("5ba4c20b7f8b526e5ec46013"), "title" : "mongodb overview", "description" : "MongoDB is no sql database",
"by_user" : "xyz", "likes" : 10 }
{ "_id" : ObjectId("5ba4c2687f8b526e5ec46014"), "title" : "nosql overview", "description" : "no sql is very fast", "by_user" : "xyz",
"likes" : 100 }
{ "_id" : ObjectId("5ba4c2c27f8b526e5ec46015"), "title" : "indexing overview", "description" : "index helps in search queries",
"by_user" : "pqr", "likes" : 10 }

> db.mahie.aggregate([{$group:{_id:"$by_user",num_total:{$sum:"$likes"}}}])
{ "_id" : "pqr", "num_total" : 10 }
{ "_id" : "xyz", "num_total" : 110 }
>  db.mahie.aggregate([{$group:{_id:"$by_user",num_total:{$avg:"$likes"}}}])
{ "_id" : "pqr", "num_total" : 10 }
{ "_id" : "xyz", "num_total" : 55 }
> db.mahie.aggregate([{$group:{_id:"$by_user",num_total:{$min:"$likes"}}}])
{ "_id" : "pqr", "num_total" : 10 }
{ "_id" : "xyz", "num_total" : 10 }
> db.mahie.aggregate([{$group:{_id:"$by_user",num_total:{$max:"$likes"}}}])
{ "_id" : "pqr", "num_total" : 10 }
{ "_id" : "xyz", "num_total" : 100 }
> db.mahie.aggregate([{$group:{_id:"$by_user",url:{$push:"$likes"}}}])
{ "_id" : "pqr", "url" : [ 10 ] }
{ "_id" : "xyz", "url" : [ 10, 100 ] }
> db.mahie.find().pretty()
{
    "_id" : ObjectId("5ba4c20b7f8b526e5ec46013"),
    "title" : "mongodb overview",
    "description" : "MongoDB is no sql database",
    "by_user" : "xyz",
    "likes" : 10
}
{
    "_id" : ObjectId("5ba4c2687f8b526e5ec46014"),
    "title" : "nosql overview",
    "description" : "no sql is very fast",
    "by_user" : "xyz",
    "likes" : 100
}
{
    "_id" : ObjectId("5ba4c2c27f8b526e5ec46015"),
    "title" : "indexing overview",
    "description" : "index helps in search queries",
    "by_user" : "pqr",
    "likes" : 10
}
> db.mahie.aggregate([{$group:{_id:"$by_user",url:{$push:"$likes"}}}])
{ "_id" : "pqr", "url" : [ 10 ] }
{ "_id" : "xyz", "url" : [ 10, 100 ] }
> db.mahie.aggregate([{$group:{_id:"$by_user",url:{$addToSet:"$likes"}}}])
{ "_id" : "pqr", "url" : [ 10 ] }
{ "_id" : "xyz", "url" : [ 100, 10 ] }

You might also like