Sunday 13 January 2019

MongoDB

mongodb


MongoDB is an open-source document database and leading NoSQL database.
It is written in C++.
It is a cross-platform.
Document oriented data based.
that provides, high performance, high availability, and easy scalability.It works on concept of collection
and document.

Basic syntax of use DATABASE statement is as follows −
.use DATABASE_NAME 
use music


Your created database (mydb) is not present in list.
To display database, you need to insert at least one document into it.
db.movie.insert({"name":" songdetails"})


MongoDB db.dropDatabase() command is used to drop a existing database.

db.dropDatabase()

To check the list of available databases by using the command, show dbs.

>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB


MongoDB db.createCollection(name, options) is used to create collection.

db.createCollection(name, options)

MongoDB's db.collection.drop() is used to drop a collection from the database.

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example:


1) Create a Database called music​.


  • use music

2) Create a collection called songdetails​.


  • db.createCollection("songdetails")


3) Create the above 5 song documents.


db.songdetails.insert({"Song Name":"Thaniye Thananthaniye","Film":"Rhythm","Music Director":"A.R.Rahman","Singer":"Shankar mahadevan"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Evano Oruvan","Film":"Alai Payuthey","Music Director":"A.R.Rahman","Singer":"Swarnalatha"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Roja Poonthottam","Film":"Kannukkul Nilavu","Music Director":"Ilaiyaraja","Singer":"Unnikirishnan,Anuradha Sriram"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Vennilave Vennilave vinnaithandi","Film":"Minsara Kanavu","Music Director":"A.R.Rahman","Singer":"Hariharan,Sadhana Sargam"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Sollamal Thottu Chellum Thendral","Film":"Dheena","Music Director":"Yuvan Shankar Raja","Singer":"Hariharan"})













4) List all documents created.
  • db.songdetails.find().pretty()



5) List A.R.Rahman’s songs.


  • db.songdetails.find({"Music Director":"A.R.Rahman"})



6) List A.R.Rahman’s songs sung by Unnikrishnan.


  • db.songdetails.find({"Music Director":"Ilaiyaraja","Singer":"Unnikirishnan,Anuradha Sriram"},{"Song Name":1,_id:0})

7) Delete the song which you don’t like.
  • db.songdetails.remove({"Song Name":"Evano Oruvan"})



8) Add new song which is your favourite.
  • db.songdetails.insert({"Song Name":"Kannana Kanne","Film":"Viswasam","Music Director":"D.imman","Singer":"Sid Sriram"})

9) List Songs sung by Sid sriram from Viswasam film.


  • db.songdetails.find({"Film":"Viswasam","Singer":"Sid Sriram"},{"Song Name":1,_id:0})



10)List out the singers’ names in your document.


  • db.songdetails.find({},{"Singer":1,_id:0}).pretty()





example2




1) Create a Database called student
use student


2) Create a collection called ​studentmarks
db.creatCollection(“studentmarks”)

3) Create the documents listed in above table.

db.songdetails.insert({"name":"Malal","maths_marks":45,"english_marks":53,"science_marks":72})
.
.
.

4) Increase the maths marks of Mala by 6 marks







5) List the names of students who got more than 50 marks in Maths Subject.


6)Add a new column(field) for Average for all students.
db.studentmarks.aggregate({$addFields:{“avarage”:””}}).pretty()              

7) Update Marks_Science=75 to Lucky .
db.studentmarks.update({“name”:”Lucky”},{$set:{science_marks”:75}})





8) List the names who got more than 50 marks in all subjects.

db.studentmarks.find({$and:[{"english_marks":{$gt:50}},{"science_marks":{$gt:50}},{"maths_marks":{$gt:50}}]},{"name":1,_id:0})



9) List the names who got less than 50 marks in Maths subject and more than 50 marks in English
db.studentmarks.find({$and:[{"english_marks":{$gt:50}},{"maths_marks":{$lt:50}}]},{"name":1,_id:0})



10) List the names who got less than 40 in both Maths and Science.

db.studentmarks.find({$and:[{"science_marks":{$lt:40}},{"maths_marks":{$lt:40}}]},{"name":1,_id:0})
11) Remove Science column/field for Raam
db.studentmarks.update({"name":"Raam"},{$unset:{"science_marks":88}})


12) Update John’s Math mark as 87 and English mark as 23, if john not available upsert.

db.studentmarks.insert({"name":"John","maths_marks":87,"english_marks":23})

13) Rename the english_marks column/field for John to science_marks

db.studentmarks.update({"name":"John"},{$rename:{"english_marks":"science_marks"}})




14) Remove Kumaran’s document from collection

db.studentmarks.remove({"name":"Kumaran"})


15) Find Kala’s or Aruli’s math_marks and science_marks

db.studentmarks.find({"name":"Aruli"},{"maths_marks":1,"science_marks":1}).pretty()