Total Pageviews

Sunday, January 22, 2017

CRUD Operation in MongoDB




1. Open a Windows Command Prompt.

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

3. You will see your console as below:











4. You see a test database connected.

5. Type “db” to find the current database, and it will be“test” database.

6. Use, “cls” or “ctrl-l” to clean the screen.

7. Switch from one database to another: “use <<dbname>>” command.

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











9. To see the list of databases, write following command:
 “show databases”






10. In mongo db, data is stored in collection, in the format of document. To show the list of collections in a database, write the following command:
 “show collections”






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







12. To find a document inserted into a collection, write the following command:
"db.avi.find()"








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






13. To modify a document, write the following command:
"db.avi.save({“_id”:1, pincode: 844502})"








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

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








15. To remove a document from a collection, write the following remove command:
"db.avi.remove({_id:1})"







16. To remove a collection from a db, write the following drop command:
"db.avi.drop()"







17. To drop a database, write the following drop-database command:
"db.dropdatabase()"







No comments:

Post a Comment