Sunday, 25 December 2016

CRUD Operation in Mongo DB

              

1.1. Go to the mongo db installation path, and open a console.

1.2. Write  mongo in the console and hit <<enter>>.

1.3. You will see your console as below:









1.4. You see a test database connected.

1.5. You may also type “db” and find the current database you are in, and hence you will find “test” database.

1.6. We may use, “cls” or “ctrl-l” to clean the screen.

1.7. You may switch from one database to another using “use <<dbname>>” command.

1.8. If we use a command “use avinash”, in the same console, then it will go to the “avinash” database.











1.9. If we want to see the list of databases, we can use “show databases” command,
      






2.0. In mongo db, data is stored in collection, in the format of document. To show the list of collection in a database, use command “show collections
       





2.1.To insert a document in a collection named “avi”, the command is:
   "db.avi.insert({ name:”avinash”, location:”bangalore”})"
          







2.2. To find a document inserted into a collection, the command is:
"db.avi.find()"
                  







 "db.avi.find({“_id :1”})"
   





2.3. To modify a document, we need to use save command:
   "db.avi.save({“_id”:1, pincode: 844502})"








  Note: If we use save , then it will modify the complete document with the new details provided by save command.

2.4. Hence, to update the document , we need to use update command:
   "db.avi.update{{_id:1}, { $set : { name: “avinash”}})"








2.5. To remove a document from a collection, we need to use remove command:
   "db.avi.remove({_id:1})"
             






2.6. To remove a collection from a db, need to use drop command:
   "db.avi.drop()"
          
2.7. To drop a database, we need to use dropdatabase command:
   "db.dropdatabase()"




No comments:

Post a Comment