0% found this document useful (0 votes)
29 views14 pages

Viraj NGT Practical

The document describes MongoDB queries to perform various operations: 1) Create and drop databases and collections, and insert, query, update, and delete documents. 2) Use selectors, projectors, sorting, limiting, skipping, and findOne to query documents. 3) Implement aggregation using expressions like sum, avg, min, max, push, addToSet, first, and last.

Uploaded by

nighotviraj55
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)
29 views14 pages

Viraj NGT Practical

The document describes MongoDB queries to perform various operations: 1) Create and drop databases and collections, and insert, query, update, and delete documents. 2) Use selectors, projectors, sorting, limiting, skipping, and findOne to query documents. 3) Implement aggregation using expressions like sum, avg, min, max, push, addToSet, first, and last.

Uploaded by

nighotviraj55
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/ 14

Practical no – 1

MongoDB Basics

a. Write a MongoDB query to create and drop database.

b. Write a MongoDB query to create, display and drop

collection.

c. Write a MongoDB query to insert, query, update and delete

a document.

AIM 1.a) Write a MongoDB query to create and drop database.

Syntax :

Create –

use database_name

Drop –

db.dropDatabase()

Source Code:

Use mydb

db.createCollection("table")

db.dropDatabase()

OUTPUT :

Create database -
Drop Database –

AIM 1.B ) Write a MongoDB query to create, display and drop

collection.

Syntax:

Create -

db.createCollection(collection_name)

db.collection_name.insert({key:value})

Display –

db.collection_name.find()

Drop -

db.collection_name.drop()

Source code:

use mydb

db.createCollection(“students”)

db.students.insert({“name:”viraj”,”RollNo”:5,Class:”A”,”gend

er”:”M”})
show collections

db.students.find()

db.students.drop()

output-

AIM 1.C) Write a MondoDB query to insert, query, update

and delete a document.

Syntax:

To Insert Document:

db.COLLECTION_NAME.insert(document)

To Query Document:

db.COLLECTION_NAME.ine()

To Update Document:

db.COLLECTION_NAME.update(SELECTION_CRITERIA,UPDATED

DATA)

To Delete Document:

db.COLLECTION_NAME.remove(DELETION_CRITERIA)

OUTPUT :
Insert documentation –

Query Document –

UPDATE DOCUMENT –

DELETE DOCUMENT –

PRACTICAL NO:02
Simple Queries with MongoDB

1) Selector –

Syntax :

db.collection_name.find({"Key":"Value"})

Source code:

Use tyit

db.student.find({"Gender":"M"})

OUTPUT :

2 ) Projector -

Syntax:

db.collection_name.find({"Key":"Value"},

{"Key":Value,"Key":Value})

Source code:

Use MYBase

db.student.find({"Gender":"M"},{"Name":1,"Age":1})

Output:

3 ) sort() -
Syntax:

db.collection_name.find({"Key":"Value"},

{"Key":Value,"Key":Value}).sort({"Key":Value})

Source code:

Use MYBase

db.students.find({"Gender":"M"},{"Name":1,"Age":1}).sort({"Age":1})

OUTPUT:

4 ) Limit() -

Syntax:

db.collection_name.find({"Key":"Value",$or:[{"Key":"Value"},

{"Key":" Value"}]}).limit(Value)

Source code:

Use tyit

db.stud.find({"gender":"F",$or:[{"class":"a"},{"score":"70"}

]}).limit(2)

Output:

5 ) Skip() -
Syntax:

db.collection_name.find({"Key":"Value",$or:[{"Key":"Value"},

{"Key":" Value"}]}).limit(Value).skip(Value)

Source code:

Use tyit

db.stud.find({"Gender":"F",$or:[{"class":"a"},

{"score":"70"}]}).limit(2).skip(2)

Output:

6 ) Findone() -

Syntax:

db.collection_name.findOne({"Key":"Value"},

{"Key":Value,"Key":Value})

db.collection_name.findOne()

Source code:

Use tyit

db.stud.findOne({"gender":"F"}, {"Name":1,"Age":1})

db.stud.findOne()

Output:

7) ensureIndex

Syntax:
db.collection_name.ensureIndex({"Key":"Value"})

Source Code:

Use mydatabase

Show collections

db.example.ensureIndex({“age”:26})

OUTPUT-

8 ) Pretty() -

Syntax :

db.collection_name.find().pretty()

Source Code:

use tyit

show collections

db.user1.find() db.user1.find.pretty()

OUTPUT-

9 ) Conditional Operators -

Syntax:

$lt and $lte


db.collection_name.find({"Key":{"$lt":Value}})

db.collection_name.find({"Key":{"$lte":Value}})

$gt and $gte

db.collection_name.find({"Key":{"$gt":Value}})

db.collection_name.find({"Key":{"$gte":Value}})

$in and $nin

db.collection_name.find({"Key":{"$in":["Value","Value"]}})

db.collection_name.find({"Key":{"$nin":["Value","Value"]}})

Source code:

use tyit

db.createCollection(“stud”)

db.stud.insert({Name:"S1",Age:25,Gender:"M",Class:"C1",Sc

ore:95})

db.stud.insert({Name:"S2",Age:18,Gender:"M",Class:"C1",Sc

ore:85})

db.stud.insert({Name:"S3",Age:18,Gender:"F",Class:"C1",Sco

re:85})

db.stud.insert({Name:"S4",Age:18,Gender:"F",Class:"C1",Sco

re:75})

db.stud.insert({Name:"S5",Age:18,Gender:"F",Class:"C2",Sco

re:75})

db.stud.insert({Name:"S6",Age:21,Gender:"M",Class:"C2",Sc
ore:100})

db.stud.insert({Name:"S7",Age:21,Gender:"M",Class:"C2",Sc

ore:100})

db.stud.insert({Name:"S8",Age:25,Gender:"F",Class:"C2",Sco

re:100})

db.stud.insert({Name:"S9",Age:25,Gender:"F",Class:"C2",Sco

re:90})

db.stud.insert({Name:"S10",Age:28,Gender:"F",Class:"C3",Sc

ore:90})

db.stud.find()

$lt and $lte db.stud.find({"Age":{"$lt":25}})

db.stud.find({"Age":{"$lte":25}})

$gt and $gte db.stud.find({"Age":{"$gt":25}})

db.stud.find({"Age":{"$gte":25}})

$in and $nin

db.stud.find({"Class":{"$in":["C1","C2"]}})

db.stud.find({"Class":{"$nin":["C1","C2"]}})

Output:

$lt and $lte :


$gt and $gte :

PRACTICAL NO:03
Implementing Aggregation

a.Write a MongoDB query to use sum, avg, min and max

expression.

b.Write a MongoDB query to use push and addToSet

expression.

c.Write a MongoDB query to use first and last expression.

3 ) a - Write a MongoDB query to use sum, avg, min and max

expression.

Syntax:

Sum

{$sum:[<expression1>,<expression>…]}

Avg

{$avg:[<expression1>…]}

Min

{$min:[<expression1>…]}

Max

{$Max:[<expression1>…]}

Source code:

use minimum

db.createCollection(“Sales”)

db.Sales.insert({ "_id" : 1, "item" : "abc", "price" : 10,

"quantity" : 2,
"date" : ISODate("2014-01-01T08:00:00Z") }

db.Sales.insert { "_id" : 2, "item" : "jkl", "price" : 20,

"quantity" : 1,

"date" : ISODate("2014-02-03T09:00:00Z") }

db.Sales.insert { "_id" : 3, "item" : "xyz", "price" : 5, "quantity"

: 5,

"date" : ISODate("2014-02-03T09:05:00Z") }

db.Sales.insert { "_id" : 4, "item" : "abc", "price" : 10,

"quantity" : 10,

"date" : ISODate("2014-02-15T08:00:00Z") }

db.Sales.insert { "_id" : 5, "item" : "xyz", "price" : 5, "quantity"

: 10,

"date" : ISODate("2014-02-15T09:05:00Z") }

db.sales.aggregate([{$group:{_id:"$item",sum:{$sum:"$price"

}}}])

db.sales.aggregate([{$group:{_id:"$item",Avg:{$avg:"$quantit

y"}}}])

db.sales.aggregate([{$group:{_id:"$item",Min:{$min:"$quanti

ty"}}}])

db.sales.aggregate([{$group:{_id:"$item",Max:{$max:"$quant

ity"}}}])

OUTPUT :

You might also like