List Of Mongo Database Commands And Methods

Below is the list of Mongo Database usefull commands -

1.   use DATABASE   :      The command will create a new database, if it doesn't exist otherwise it will return the existing database.

2.    show dbs             :     To check the list of all your databases.

3.       Your created database (mydb) is not present in list. To display database you need to insert atleast one document into it. Run below command

db.movie.insert({"name":"tutorials point"})

4.       db.dropDatabase() : This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.

5.       createCollection(name, option)  : In the method, name is name of collection to be created. Options is a document and used to specify configuration of collection.

6.       show collections  : You can check the created collection(tables) by using this command.

7.       db.collection_name.drop() : This method is used to drop a collection from the database. drop() method will return true, if the selected collection is dropped successfully otherwise it will return false.

8.       insert() or save()  :

Syntax : db.COLLECTION_NAME.insert(document)

Example :

db.mycol.insert({

   _id: ObjectId(7df78ad8902c),

   title: 'MongoDB Overview',

   description: 'MongoDB is no sql database',

   by: 'tutorials point',

   url: 'http://www.tutorialspoint.com',

   tags: ['mongodb', 'database', 'NoSQL'],

   likes: 100

})


9.   find()  : This method will display all the documents in a non structured way.

Conditions of find() method :

Operation
Syntax
Example
RDBMS Equivalent
Equality
{<key>:<value>}
db.mycol.find({"by":"tutorials point"}).pretty()
where by = 'tutorials point'
Less Than
{<key>:{$lt:<value>}}
db.mycol.find({"likes":{$lt:50}}).pretty()
where likes < 50
Less Than Equals
{<key>:{$lte:<value>}}
db.mycol.find({"likes":{$lte:50}}).pretty()
where likes <= 50
Greater Than
{<key>:{$gt:<value>}}
db.mycol.find({"likes":{$gt:50}}).pretty()
where likes > 50
Greater Than Equals
{<key>:{$gte:<value>}}
db.mycol.find({"likes":{$gte:50}}).pretty()
where likes >= 50
Not Equals
{<key>:{$ne:<value>}}
db.mycol.find({"likes":{$ne:50}}).pretty()
where likes != 



10.   pretty() : To display the results in a formatted way.

11.   AND in MongoDB  :

Syntax : In the find() method if you pass multiple keys by separating them by ',' then MongoDB treats it AND condition. Basic syntax of AND is shown below :

db.mycol.find({key1:value1, key2:value2}).pretty()

Example :

db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty()


12.               OR in MongoDB :

Syntax: To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below –

db.mycol.find(

   {

      $or: [

         {key1: value1}, {key2:value2}

      ]

   }

).pretty()


Example : db.mycol.find({$or:[{"by":"tutorials point"}, {"title": "MongoDB Overview"}]}).pretty()
 

13.  update()  : This method updates values in the existing document.

Syntax : db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example : db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})

Note : By default mongodb will update only single document, to update multiple you need to set a paramter 'multi' to true.

Syntax : db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})

14.   remove() : This method is used to remove document from the collection. remove()    method accepts two parameters. One is deletion criteria and second is justOne flag –

Syntax : db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Example : db.mycol.remove({'title':'MongoDB Overview'})

Remove only one : If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method.

Syntax : db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

Remove All documents :  If you don't specify deletion criteria, then mongodb will delete whole documents from the collection. This is equivalent of SQL's truncate command.


Chears :)
Happy Coding

Post a Comment

Previous Post Next Post