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

20mia1061 (Yash Nair) - Nosql Lab-7

The document discusses several NoSQL concepts using a MongoDB database: 1) A capped collection is created to store student data and its size is checked. 2) Documents are inserted into a normal collection, which is then converted to a capped collection to analyze size differences. 3) A TTL index is created on a collection and its behavior after expiration is shown.

Uploaded by

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

20mia1061 (Yash Nair) - Nosql Lab-7

The document discusses several NoSQL concepts using a MongoDB database: 1) A capped collection is created to store student data and its size is checked. 2) Documents are inserted into a normal collection, which is then converted to a capped collection to analyze size differences. 3) A TTL index is created on a collection and its behavior after expiration is shown.

Uploaded by

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

20MIA1061(YASH NAIR)_NOSQL LAB-7

Create a new capped collection to store the students data and check the size property –
how it works?

db.createCollection("MIA1061",{capped:true,size:1024})

db.MIA1061.insertMany([
{name:"Yash",type:"dayscholar",branch:"MIA",year:4,address:"Egmore,Chennai"},
{name:"Rahul",type:"Hosteller",branch:"MIA",year:3,block:"A",address:"Noida,New Delhi"},
{name:"Hareesh",type:"dayscholar",branch:"MIS",address:"Egmore,Chennai"},
{name:"Sivakumar",type:"Hosteller",branch:"MIA",address:"Velachery,Chennai"},
{name:"Nithya",type:"Hosteller",branch:"MIA",year:3,block:"B",address:"Andheri,Mumbai"}
])

db.MIA1061.dataSize()

Convert the normal collection to a capped collection and analyze how it works in case the
size is different between two collections.

db.MIA1061.insertMany([
{name:"Yash",type:"dayscholar",branch:"MIA",year:4,address:"Egmore,Chennai"},
{name:"Rahul",type:"Hosteller",branch:"MIA",year:3,block:"A",address:"Noida,New Delhi"},
{name:"Hareesh",type:"dayscholar",branch:"MIS",address:"Egmore,Chennai"},
{name:"Sivakumar",type:"Hosteller",branch:"MIA",address:"Velachery,Chennai"},
{name:"Nithya",type:"Hosteller",branch:"MIA",year:3,block:"B",address:"Andheri,Mumbai"}
])
db.runCommand({"convertToCapped": "MIA1061", size:5000,max:3});
db.MIA1061.isCapped()
db.MIA1061.find()

Create a collection with ttl index and show the execution after time expires.

db.MIA1061.insertMany([
{name:"Yash",type:"dayscholar",branch:"MIA",year:4,address:"Egmore,Chennai"},
{name:"Rahul",type:"Hosteller",branch:"MIA",year:3,block:"A",address:"Noida,New Delhi"},
{name:"Hareesh",type:"dayscholar",branch:"MIS",address:"Egmore,Chennai"},
{name:"Sivakumar",type:"Hosteller",branch:"MIA",address:"Velachery,Chennai"},
{name:"Nithya",type:"Hosteller",branch:"MIA",year:3,block:"B",address:"Andheri,Mumbai"}
])

db.MIA1061.createIndex( { "type": 1 }, {expireAfterSeconds: 30 } )


Convert the size property of existing capped collection to n if it is capped collection.

db.createCollection("MIA1061",{capped:true,size:1024})

db.MIA1061.insertMany([
{name:"Yash",type:"dayscholar",branch:"MIA",year:4,address:"Egmore,Chennai"},
{name:"Rahul",type:"Hosteller",branch:"MIA",year:3,block:"A",address:"Noida,New Delhi"},
{name:"Hareesh",type:"dayscholar",branch:"MIS",address:"Egmore,Chennai"},
{name:"Sivakumar",type:"Hosteller",branch:"MIA",address:"Velachery,Chennai"},
{name:"Nithya",type:"Hosteller",branch:"MIA",year:3,block:"B",address:"Andheri,Mumbai"}
])

db.MIA1061.isCapped();
db.MIA1061.find();

db.runCommand({"collMod":"MIA1061", cappedSize:2000})
db.MIA1061.find();

Create a collection for employee details with dept as an index key with hide property to
analyze the performance.

db.MIA1061.createIndex({"branch":1},{hidden:true})

db.MIA1061.find({branch:"MIA"}).explain("executionStats");
{
explainVersion: '1',
queryPlanner: {
namespace: 'mycompiler_mongodb.MIA1061',
indexFilterSet: false,
parsedQuery: { branch: { '$eq': 'MIA' } },
queryHash: 'BE24B5FA',
planCacheKey: 'B3CE13BA',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'COLLSCAN',
filter: { branch: { '$eq': 'MIA' } },
direction: 'forward'
},
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 4,
executionTimeMillis: 0,
totalKeysExamined: 0,
totalDocsExamined: 5,
executionStages: {
stage: 'COLLSCAN',
filter: { branch: { '$eq': 'MIA' } },
nReturned: 4,
executionTimeMillisEstimate: 0,
works: 7,
advanced: 4,
needTime: 2,
needYield: 0,
saveState: 0,
restoreState: 0,
isEOF: 1,
direction: 'forward',
docsExamined: 5
}
},
command: {
find: 'MIA1061',
filter: { branch: 'MIA' },
'$db': 'mycompiler_mongodb'
},
serverInfo: {
host: '169.254.202.97',
port: 27017,
version: '6.0.5',
gitVersion: 'c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
},
ok: 1
}

Display the highest salary department wise using python and mongodb.

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["db"]
collection = db["Employee"]

Employee = collection.find()
for employee in Employee:
print(employee)

pipeline = [
{
"$group": {
"_id":"$Dept",
"max_salary":{"$max":"$Salary"}
}
}
]

result = list(collection.aggregate(pipeline))

for item in result:


print(f"Dept: {item['_id']}, Highest Salary: {item['max_salary']}")

client.close()
{'_id': ObjectId('650de848fce549fa25551655'), 'ID': 1, 'Experience_Years': 5, 'Age': 28,
'Gender': 'Female', 'Salary': 250000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa25551656'), 'ID': 2, 'Experience_Years': 1, 'Age': 21,
'Gender': 'Male', 'Salary': 50000, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551657'), 'ID': 3, 'Experience_Years': 3, 'Age': 23,
'Gender': 'Female', 'Salary': 170000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa25551658'), 'ID': 4, 'Experience_Years': 2, 'Age': 22,
'Gender': 'Male', 'Salary': 25000, 'Dept': 'Mechanical'}
{'_id': ObjectId('650de848fce549fa25551659'), 'ID': 5, 'Experience_Years': 1, 'Age': 17,
'Gender': 'Male', 'Salary': 10000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa2555165a'), 'ID': 6, 'Experience_Years': 25, 'Age': 62,
'Gender': 'Male', 'Salary': 5001000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa2555165b'), 'ID': 7, 'Experience_Years': 19, 'Age': 54,
'Gender': 'Female', 'Salary': 800000, 'Dept': 'Mechanical'}
{'_id': ObjectId('650de848fce549fa2555165c'), 'ID': 8, 'Experience_Years': 2, 'Age': 21,
'Gender': 'Female', 'Salary': 9000, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa2555165d'), 'ID': 9, 'Experience_Years': 10, 'Age': 36,
'Gender': 'Female', 'Salary': 61500, 'Dept': 'Marketing'}
{'_id': ObjectId('650de848fce549fa2555165e'), 'ID': 10, 'Experience_Years': 15, 'Age': 54,
'Gender': 'Female', 'Salary': 650000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa2555165f'), 'ID': 11, 'Experience_Years': 4, 'Age': 26,
'Gender': 'Female', 'Salary': 250000, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa25551660'), 'ID': 12, 'Experience_Years': 6, 'Age': 29,
'Gender': 'Male', 'Salary': 1400000, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551661'), 'ID': 13, 'Experience_Years': 14, 'Age': 39,
'Gender': 'Male', 'Salary': 6000050, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551662'), 'ID': 14, 'Experience_Years': 11, 'Age': 40,
'Gender': 'Male', 'Salary': 220100, 'Dept': 'Marketing'}
{'_id': ObjectId('650de848fce549fa25551663'), 'ID': 15, 'Experience_Years': 2, 'Age': 23,
'Gender': 'Male', 'Salary': 7500, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa25551664'), 'ID': 16, 'Experience_Years': 4, 'Age': 27,
'Gender': 'Female', 'Salary': 87000, 'Dept': 'Mechanical'}
{'_id': ObjectId('650de848fce549fa25551665'), 'ID': 17, 'Experience_Years': 10, 'Age': 34,
'Gender': 'Female', 'Salary': 930000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa25551666'), 'ID': 18, 'Experience_Years': 15, 'Age': 54,
'Gender': 'Female', 'Salary': 7900000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa25551667'), 'ID': 19, 'Experience_Years': 2, 'Age': 21,
'Gender': 'Male', 'Salary': 15000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa25551668'), 'ID': 20, 'Experience_Years': 10, 'Age': 36,
'Gender': 'Male', 'Salary': 330000, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551669'), 'ID': 21, 'Experience_Years': 15, 'Age': 54,
'Gender': 'Male', 'Salary': 6570000, 'Dept': 'Mechanical'}
{'_id': ObjectId('650de848fce549fa2555166a'), 'ID': 22, 'Experience_Years': 4, 'Age': 26,
'Gender': 'Male', 'Salary': 25000, 'Dept': 'Marketing'}
{'_id': ObjectId('650de848fce549fa2555166b'), 'ID': 23, 'Experience_Years': 5, 'Age': 29,
'Gender': 'Male', 'Salary': 6845000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa2555166c'), 'ID': 24, 'Experience_Years': 1, 'Age': 21,
'Gender': 'Female', 'Salary': 6000, 'Dept': 'Marketing'}
{'_id': ObjectId('650de848fce549fa2555166d'), 'ID': 25, 'Experience_Years': 4, 'Age': 23,
'Gender': 'Female', 'Salary': 8900, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa2555166e'), 'ID': 26, 'Experience_Years': 3, 'Age': 22,
'Gender': 'Female', 'Salary': 20000, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa2555166f'), 'ID': 27, 'Experience_Years': 1, 'Age': 18,
'Gender': 'Male', 'Salary': 3000, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa25551670'), 'ID': 28, 'Experience_Years': 27, 'Age': 62,
'Gender': 'Female', 'Salary': 10000000, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551671'), 'ID': 29, 'Experience_Years': 19, 'Age': 54,
'Gender': 'Female', 'Salary': 5000000, 'Dept': 'Finance'}
{'_id': ObjectId('650de848fce549fa25551672'), 'ID': 30, 'Experience_Years': 2, 'Age': 21,
'Gender': 'Female', 'Salary': 6100, 'Dept': 'Marketing'}
{'_id': ObjectId('650de848fce549fa25551673'), 'ID': 31, 'Experience_Years': 10, 'Age': 34,
'Gender': 'Male', 'Salary': 80000, 'Dept': 'IT'}
{'_id': ObjectId('650de848fce549fa25551674'), 'ID': 32, 'Experience_Years': 15, 'Age': 54,
'Gender': 'Male', 'Salary': 900000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa25551675'), 'ID': 33, 'Experience_Years': 20, 'Age': 55,
'Gender': 'Female', 'Salary': 1540000, 'Dept': 'Logistics'}
{'_id': ObjectId('650de848fce549fa25551676'), 'ID': 34, 'Experience_Years': 19, 'Age': 53,
'Gender': 'Female', 'Salary': 9300000, 'Dept': 'HR'}
{'_id': ObjectId('650de848fce549fa25551677'), 'ID': 35, 'Experience_Years': 16, 'Age': 49,
'Gender': 'Male', 'Salary': 7600000, 'Dept': 'Mechanical'}

Output :

Dept: Finance, Highest Salary: 10000000


Dept: Logistics, Highest Salary: 6845000
Dept: IT, Highest Salary: 7900000
Dept: HR, Highest Salary: 9300000
Dept: Marketing, Highest Salary: 220100
Dept: Mechanical, Highest Salary: 7600000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy