Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CRUD OPERATIONS IN MONGO DB

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 222
    Comment on it

    Below is the list of basic MONGO DB commands which a user can start with :

    1) To list all the databases

    show dbs

    Example:

    > show dbs
    admin  (empty)
    local  0.078GB
    mydb   0.078GB

    2) To create a new database

    use dbName

    Example:

    > use mydb   
    switched to db mydb

    3) Check in the list of database that db created or not. Use cmd:

    show dbs

    Example:

    > show dbs
    admin  (empty)
    local  0.078GB
    mydb   0.078GB


    But your recently created db will not appear in the list until and unless you have not made any entry in it.
    To do the entry in the database you created, hit the below cmd:

    db.users.insert({'name':'shikha'})

    Example:

    > db.users.insert({'name':'shikha'})
    WriteResult({ "nInserted" : 1 })

    Then use the below command :

    show dbs

    Example:

    Example:
    > show dbs
    admin   (empty)
    local   0.078GB
    mydb    0.078GB
    shikha  0.078GB
    users   0.078GB

    Now your created db is appearing in the list of dbs

    4) To check currently which db you are using:

    db

    Example:

    > db
    users

    5) To switch to one db to another db, use below cmd:

    use yourDbName

    Example:

    > use user
    switched to db user

    6) To list all the collections of the current db, use the below command:

    show collections

    Example:

    > show collections
    system.indexes
    users

    7) To drop the database on which you currently are, use the below command:

    db.dropDatabases()

    Example:

    > db.dropDatabase()
    { "dropped" : "users", "ok" : 1 }

    8) To create a collection inside a db on which you are currently in, use below command:

    db.createCollection("collectionName")

    Example:

    > db.createCollection("blog")
    
    { "ok" : 1 }

    To create collection automatically just insert command with your collection name then mongo itself create a new collection.
    like:

    db.login.insert({'name': 'shikha', 'password': '123456'})

    Example:

    > db.login.insert({'name': 'shikha', 'password': '123456'})
    WriteResult({ "nInserted" : 1 })

    9) To drop a particular collection from the current db in use, hit the below cmd:

    db.collectionName.drop()

    Example:

    > show collections
    blog
    login
    system.indexes
    > db.blog.drop()
    true

    10) To insert data in the particular collection, hit the below cmd:

    db.login.insert({'name': 'shikha', 'password': '123456'})

    Example:

    > db.login.insert({'name': 'shikha', 'password': '123456'})
    WriteResult({ "nInserted" : 1 })

    To do the multiple entries at a single, hit use below cmd:

    db.login.insert({'name': 'shikha', 'password': '123456'}, {'name': 'komal', 'password': '123456'})

    Example:

    > db.login.insert({'name': 'shikha', 'password': '123456'}, {'name': 'komal', 'password': '123456'})
    WriteResult({ "nInserted" : 1 })

    11) To find a result from the particular collection, use below cmd:

    db.login.find({'name': 'shikha'})

    Example:

    > db.login.find({'name': 'shikha'})
    { "_id" : ObjectId("573473da41a5b179fd014327"), "name" : "shikha", "password" : "123456" }

    To display the result in more readable way, use below cmd:

    db.login.find({'name': 'shikha'}).pretty()

    Example:

    > db.login.find({'name': 'shikha'}).pretty()
    {
    	"_id" : ObjectId("573473da41a5b179fd014327"),
    	"name" : "shikha",
    	"password" : "123456"
    }

    12) How to use of "or" in mongo?

    Syntax with example:

        db.login.find(
           {
              $or: [
                 {name: shikha}, {password:123456}
              ]
           }
        ).pretty()
    >db.login.find({$or: [{'name': 'lalit'},{'password':'123456a'} ]}).pretty()
    {
    	"_id" : ObjectId("5734745141a5b179fd01432a"),
    	"name" : "lalit",
    	"password" : "123456a"
    }

    13) To list all the data of particular collection, hit below cmd:

    db.login.find() 

    Example:

    > db.login.find()
    { "_id" : ObjectId("5734739941a5b179fd014326"), "name" : "harneet", "password" : "123456" }
    { "_id" : ObjectId("573473da41a5b179fd014327"), "name" : "shikha", "password" : "123456" }
    { "_id" : ObjectId("5734742941a5b179fd014328"), "name" : "shikha2", "password" : "123456" }
    { "_id" : ObjectId("5734744241a5b179fd014329"), "name" : "shikha1", "password" : "123456" }
    { "_id" : ObjectId("5734745141a5b179fd01432a"), "name" : "lalit", "password" : "123456a" }

    14) To update the data of the particular entry, hit below cmd:

    db.login.update({'name':'komal'}, {$set:{'name':'mukul'}},{multi:true})

    Example:

    > db.login.update({'name':'lalit'}, {$set:{'name':'mukul'}},{multi:true})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

    15) To remove a entry a from the collection data, hit the below cmd:

     db.login.remove({'name':'nisha'})

    Example:

    > db.login.remove({'name':'shikha'})
    WriteResult({ "nRemoved" : 1 })

    16) To count the number of entries in the collection, hit the below cmd:

    db.login.count()

    Example:

    > db.login.count()
    2

     

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: