0% found this document useful (0 votes)
5 views4 pages

MongoDB Practice

The document contains a series of MongoDB commands for managing a student database named 'collegedb'. It includes operations such as inserting student records, updating marks, and querying data based on various criteria. Additionally, it demonstrates the use of aggregation to analyze student income by age and admission type.

Uploaded by

npavitra082
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)
5 views4 pages

MongoDB Practice

The document contains a series of MongoDB commands for managing a student database named 'collegedb'. It includes operations such as inserting student records, updating marks, and querying data based on various criteria. Additionally, it demonstrates the use of aggregation to analyze student income by age and admission type.

Uploaded by

npavitra082
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

Cmd> mongosh

test>
test> use collegedb
db.student.insertOne({"usn":1,"name":"alex","fathername":"sam","mothername
":"mary","hobbies":
["dancing","singing","movies"],"admissiontype":"cet","age":17,"income":4000
0,"skills":["c","c++","python"],"place":"kundapura","location":
{"type":"Point","coordinates":[-73.856077,
40.848447]},"college":"mitk","comments":"this is a great college"})

collegedb>
db.student.insertMany([{"usn":2,"name":"ravi","fathername":"rajesh","mothern
ame":"sandhya","hobbies":
["painting","singing","movies"],"admissiontype":"cet","age":18,"income":5000
0,"skills":["java","c++","aiml"],"place":"kundapura","location":
{"type":"Point","coordinates":[-72.856077,
41.848447]},"college":"mitk","comments":"good to learn new things"},
{"usn":3,"name":"kumar","fathername":"sathish","mothername":"priya","hobbi
es":
["painting","tv","movies"],"admissiontype":"mgt","age":17,"income":80000,"sk
ills":["java","c++","aiml"],"place":"kundapura","location":
{"type":"Point","coordinates":[-71.856077,
42.848447]},"college":"aj","comments":"great place to groom talent"},
{"usn":4,"name":"lingesh","fathername":"vinoth","mothername":"nirmala","ho
bbies":
["painting","tv","movies"],"admissiontype":"cet","age":18,"income":80000,"ski
lls":["aiml","c++","java"],"place":"kundapura","location":
{"type":"Point","coordinates":[-70.856077,
40.848447]},"college":"mitk","comments":"nice college"},
{"usn":5,"name":"chinmay","fathername":"lingappa","mothername":"kavitha","
hobbies":
["painting","tv","art"],"admissiontype":"cet","age":17,"income":80000,"skills":
["aiml","c++","python"],"place":"kundapura","location":
{"type":"Point","coordinates":[-70.856077,
40.848447]},"college":"mitk","comments":"nice college"}])
collegedb> db.student.find()

collegedb> db.student.find({"usn":1})

collegedb> db.student.find().limit(2)

collegedb> db.student.find().skip(1).limit(2)

collegedb> db.student.find({"usn":1},{"usn":1,"name":1,"_id":0})

collegedb> db.student.updateOne({"usn":1},{$set:{"marks":
[88,98,100,95,91]}})

collegedb> db.student.find({"usn":1},{"usn":1,"name":1,"marks":1,"_id":0})

Pop the last element from marks array of Usn :1

collegedb> db.student.updateOne({"usn":1},{$pop:{"marks":-1}})

collegedb> db.student.find({"usn":1},{"usn":1,"name":1,"marks":1,"_id":0})

collegedb> db.student.updateOne({"usn":1},{$push:{"marks":66}})

collegedb> db.student.find({"usn":1},{"usn":1,"name":1,"marks":1,"_id":0})

collegedb> db.student.updateOne({"usn":2},{$set:{"marks":
[95,88,77,45,55]}})
collegedb> db.student.find({"usn":2},{"usn":1,"name":1,"marks":1,"_id":0})

collegedb> db.student.find({"marks":{$elemMatch:{$gt:99}}})

collegedb> db.student.find({"marks":{$elemMatch:{$gt:55,$lt:70}}})

collegedb> db.student.find({"marks.0":{$eq:100}})

collegedb> db.student.updateMany({},{$push:{"hobbies":{$each:
["baking","surfing"]}}})

collegedb> db.student.find()

collegedb> db.student.updateOne({"usn":1},{$push:{"skills":{$each:
["it","testing"],$sort:1}}})
collegedb> db.student.createIndex({"location":"2dsphere"})
db.student.find({"location":{$near:{$geometry:{type:"Point",coordinates:[-
70.856077,40.848447]},$maxDistance:10000,$minDistance:0}}}

collegedb> db.student.createIndex({"comments":"text"})
collegedb> db.student.find({$text:{$search:"great"}})
collegedb> db.student.find({$text:{$search:"\"great college\""}})

collegedb> db.student.find({$text:{$search:"great -college"}})

collegedb> db.student.aggregate([{$group:{"_id":"$age", averageIncome:


{$avg:"$income"}}}])
collegedb> db.student.aggregate([{$group:{"_id":"$admissiontype",
averageIncome:{$avg:"$income"}}}])

collegedb> db.student.aggregate([{$group:{"_id":"$admissiontype",
minIncome:{$min:"$income"}}}])

collegedb> db.student.aggregate([{$group:{"_id":"$admissiontype",
maxIncome:{$max:"$income"}}}])

db.student.aggregate([{$match:{"age":{$lt:25}}},{$sort:{"age":1}},{$skip:1},
{$project:{"_id":0,"usn":1,"name":1,"age":1}}])

You might also like