Mongo DB Commands


Mongo  DB commands for database ............................


1. view all databases ------------------- show dbs

2. create a new database or select ---------------- use dbname

3. view currunt database ---------------- db

4. delete database ---------------------- db.dropDatabase()


Commands for collections .......................................


5. create new collection ---------------- db.createCollection('collection name')

6. show all collections ----------------- show collections

7. delete collection -------------------- db.collection name.drop()


commands for Rows 


8. Insert a one row ---------------------  db.collection name.insert({

                                                             

                                                              'name': 'Arman',

                                                              'course': 'java',

                                                              'duration_month': 6


                                                               })


9. Insert many rows --------------------- db.collection name.insert([

                                                             

                                                             {

                                                              'name': 'Arman',

                                                              'course': 'java',

                                                              'duration_month': 6

                                                              },


                                                              {

                                                              'name': 'Ali',

                                                              'course': 'python',

                                                              'duration_month': 4

                                                              },


                                                              {

                                                              'name': 'Rahul',

                                                              'course': 'C',

                                                              'duration_month': 2

                                                              }

                                                               ])


10. show rows in a collection ----------- db.collection name.find()


11. show rows into a collection in pretty format ----------- db.collection name.find().pretty()


12. search in a DB ---------------------- db.example.find({field name : value})


13. Limit the number of rows in output -- db.collection name.find().limit(value)        // limit(2)


14. Count the rows in collection -------- db.collection name.find().count()


OR. Count the rows in collection -------- db.collection name.find({field name : value}).count()


15. Find the first matching row --------- db.collection name.findOne({field name : value})


16. Update a Row --------------- db.collection name.update({field name:value},


                                                        {

                                                              'name': 'Rahul',

                                                              'course': 'C programing',  //update

                                                              'duration_month': 2

                                                              })


17. Update a Row if value is not available in field then created --------------- db.collection name.update({field name:value},


                                                        {

                                                              'name': 'Rahul',

                                                              'course': 'C programing',  //update

                                                              'duration_month': 2


                                                              } ,{upsert:true})


18. Increament Operator ------------- db.collection name.update({field name:value},

                                                                        {$inc:{

                                                                               field name : value     //number like if age is 10 then give value 2 then age is 12

                                                                               }})


19. update Operator ------------- db.collection name.update({field name:value},

                                                                        {$rename:{

                                                                               field name : value     //field name change

                                                                               }})


20. Delete Row ----------------- db.collection name.remove({field name : value})






These are the important usable commands if you need more command then you can take the help of search engine.









Comments

Popular posts from this blog

JAVA is both compiled and an interpreted language, JDM.