Friday, February 1, 2019

Create "STUDENT" DB

Create a new MONGODB database called “STUDENT”


Implement the following SQL Queries.

1.) Create collection named "STUDENT_MASTER" and insert 10 documents with fields like Name, Grade, DOB, Hobby, City. (Hobby is an array)

  • db.STUDENT_MASTER.insert({
       _id : 1001,
       Name : "Nimit",
       Grade : "AA",
       DOB : ISODate("1997-06-19"),
       Hobby : ["Playing Hockey","Bhajan"],
       City : "Rajkot"
    })



No.
Description
Queries

Select All
db.STUDENT_MASTER.find({},{})
2
Display all the teachers
with name, subject and gender on screen
db.STUDENT_MASTER.find({},
{
"_id":0,
"Name":1,
"Grade":1,
"Hobby":1,
}).pretty()
3
Display only those documents where the grade of student is equal to AA.
db.STUDENT_MASTER.find(
{
"Grade":"A+"
},{}).pretty()
4
Update all those documents where grade of student is
AB with the new value of hobby as 'cricket.
db.STUDENT_MASTER.updateMany(
{
"Grade":"AB",
},{
$addToSet:
{
"Hobby":"cricket"
}
})
5
Sort the documents in the given collection by Grade in Ascending Order.
db.STUDENT_MASTER.find().sort(
{
   Grade:1
})
6
Delete data of all those students who were born before 1st January 2015

7
Count all students whose hobby is cricket
db.STUDENT_MASTER.find(
{
   "Hobby":"cricket"
}).count()
8
Display 2nd last record of student.
db.STUDENT_MASTER.find().skip(
   db.STUDENT_MASTER.count()-2
).limit(1)
9
Delete all students whose hobby is “Gaming"
db.STUDENT_MASTER.remove(
{   
   "Hobby":"Gaming"
})
10
Update city of students with "Rajkot" who are living in "Surat".
db.STUDENT_MASTER.updateMany(
{
   "City":"Surat"
},{
   $set:
   {
       "City":"Rajkot"
   }
})

No comments:

Post a Comment