Few useful command for MongoDB

Share It!

MongoDB is an open-source document-oriented database, and leading NoSQL database. MongoDB is a cross-platform that provides high performance, high availability, and easy scalability. MongoDB is built on an architecture of collections and documents. A document is a set of key-value pairs and are the basic unit of data in MongoDB. Collections contain sets of documents and function as the equivalent of relational database tablesx`

download

Create Database:

  • If you want to show your all databases, then run the command.
    # >show dbs
    
  • Create new Database using following command:
    # >use Database_name
    
  • Your created database (mydb) but it is not presented in a list. You need to insert at least one document to display database
    # >db.User.insert({"name":"tutorials point"})
    

    For Eg:

      >use techoims
       switched to db techoism
      >db.movie.insert({"name":"Location"})
       WriteResult({ "nInserted" : 1 })
    
  • If you want to check your currently selected database use following command:
    # >db
    
  • Drop Database:

  • If you want to delete the database, use following command:
    # >db.dropDatabase()
    

    For Eg:

     >use techoism
      switched to db techoism
     >db.dropDatabase()
      { "dropped" : "techoism", "ok" : 1 }
     >
    

    Note: If you have not selected any database, then it will delete default ‘test’ database

  • Create Collection:

  • Use below command to create collection:
    # > db.createCollection(name, options)
    

    Note:
    Name: Name of the collection to be created
    Option: Option is a document and used to specify configuration of collection

    For Eg:

       >use techoism
        switched to db techoism
       >db.movie("mycollection") 
        { "ok" : 1 }
       >
    

    For Eg:

       >db.movie("log", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
        { "ok" : 1 }
    
  • To check the created collection, use following command:
     
    # >show collections
    
  • Drop Collection:

  • If you want to drop any collection from Database, use following command:
  • # > db.COLLECTION_NAME.drop()
    

    For Eg:

      >use techoism
       switched to db techosim
      >show collections
       log
       movie
       name
       system.indexes
      >db.movie.drop()
       true
      >
    

    Insert Document:

  • To Insert data into MongoDB collection, you need to run following command:
    # >db.COLLECTION_NAME.insert(document)
    

    For Eg:

    >db.name.insert({User: 'Dennis R', description: 'Linux Administrator', by: 'tutorials point', url: 'http://www.techoism.com'})
     WriteResult({ "nInserted" : 1 })
    
  • If you want to insert multiple document using single query, use below command:
    >db.name.insert([
    {
       User: 'Steve J', 
       description: 'Wordpress Developer',
       by: 'tutorials point',
       url: 'http://www.techoism.com'
       },
    {
       User: 'Lina', 
       description: 'Magento Developer',
       by: 'tutorials point',
       url: 'http://www.techoism.com'
    }
    ])
    
  • Query Document:

  • To display the result in MongoDB, use following command:
  • # >db.COLLECTION_NAME.find()
    

    For Eg:

     >db.name.find()
      { "_id" : ObjectId("56024f64bef780ed433126c0"), "User" : "Dennis R", "description" : "Linux Administrator", "by" : "tutorials point", "url" : "http://www.techoism.com" }
      { "_id" : ObjectId("56025057bef780ed433126c1"), "User" : "Steve J", "description" : "Wordpress Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" }
      { "_id" : ObjectId("56025057bef780ed433126c2"), "User" : "Lina", "description" : "Magento Developer", "by" : "tutorials point", "url" : "http://www.techoism.com" }
    

    Note: To display the result in formatted way use following command:

    # >db.COLLECTION_NAME.find().pretty()
    

    For Eg:

     >db.name.find().pretty()
    {
            "_id" : ObjectId("56024f64bef780ed433126c0"),
            "User" : "Dennis R",
            "description" : "Linux Administrator",
            "by" : "tutorials point",
            "url" : "http://www.techoism.com"
    }
    {
            "_id" : ObjectId("56025057bef780ed433126c1"),
            "User" : "Steve J",
            "description" : "Wordpress Developer",
            "by" : "tutorials point",
            "url" : "http://www.techoism.com"
    }
    {
            "_id" : ObjectId("56025057bef780ed433126c2"),
            "User" : "Lina",
            "description" : "Magento Developer",
            "by" : "tutorials point",
            "url" : "http://www.techoism.com"
    }
    
  • If you want to display result for any specific value in proper formatt then use following command:
    # >db.Collection_Name.find({key1:value1, key2:value2}).pretty()
    

    For Eg:

     > db.name.find({"User":"Dennis R", "by":"tutorials point"}).pretty()
    {
            "_id" : ObjectId("56024f64bef780ed433126c0"),
            "User" : "Dennis R",
            "description" : "Linux Administrator",
            "by" : "tutorials point",
            "url" : "http://www.techoism.com"
    }
    

    Note: If you pass multiple keys by separating them by ‘,’ then MongoDB treats it as AND condition, as In above Example.

  • If you want to use or condition to display the result, use following command:
    # >db.Collection_Name.find(
       {
          $or: [
    	     {key1: value1}, {key2:value2}
          ]
       }
    ).pretty()
    

    For Eg:

    >db.name.find(
       {
          $or: [
    			     {"User": "Lina"}, {"description":"Magento Developer"}
          ]
       }
    ).pretty()
    {
            "_id" : ObjectId("56025057bef780ed433126c2"),
            "User" : "Lina",
            "description" : "Magento Developer",
            "by" : "tutorials point",
            "url" : "http://www.techoism.com"
    }
    
  • Reference:
    http://www.tutorialspoint.com/mongodb

    Enjoy it!

    Leave a Reply

    Your email address will not be published.