Tuesday, June 16, 2015

Mongodb shell commands

Open  mongodb shell
ram@ram-pc:~$ mongo
MongoDB shell version: 3.0.4
connecting to: test
Server has startup warnings:
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten]
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten]
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-17T09:48:50.525+0530 I CONTROL  [initandlisten]

> db.help()
DB methods:
    db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
    db.auth(username, password)
    db.cloneDatabase(fromhost)
    db.commandHelp(name) returns the help for the command
    db.copyDatabase(fromdb, todb, fromhost)
    db.createCollection(name, { size : ..., capped : ..., max : ... } )
    db.createUser(userDocument)
    db.currentOp() displays currently executing operations in the db
    db.dropDatabase()
    db.eval() - deprecated
    db.fsyncLock() flush data to disk and lock server for backups
    db.fsyncUnlock() unlocks server following a db.fsyncLock()
    db.getCollection(cname) same as db['cname'] or db.cname
    db.getCollectionInfos()
    db.getCollectionNames()
    db.getLastError() - just returns the err msg string
    db.getLastErrorObj() - return full status object
    db.getLogComponents()
    db.getMongo() get the server connection object
    db.getMongo().setSlaveOk() allow queries on a replication slave server
    db.getName()
    db.getPrevError()
    db.getProfilingLevel() - deprecated
    db.getProfilingStatus() - returns if profiling is on and slow threshold
    db.getReplicationInfo()
    db.getSiblingDB(name) get the db at the same server as this one
    db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
    db.hostInfo() get details about the server's host
    db.isMaster() check replica primary status
    db.killOp(opid) kills the current operation in the db
    db.listCommands() lists all the db commands
    db.loadServerScripts() loads all the scripts in db.system.js
    db.logout()
    db.printCollectionStats()
    db.printReplicationInfo()
    db.printShardingStatus()
    db.printSlaveReplicationInfo()
    db.dropUser(username)
    db.repairDatabase()
    db.resetError()
    db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
    db.serverStatus()
    db.setLogLevel(level,<component>)
    db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
    db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
    db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
    db.setVerboseShell(flag) display extra information in shell output
    db.shutdownServer()
    db.stats()
    db.version() current version of the server
>

> db.stats()
{
    "db" : "test",
    "collections" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "ok" : 1
}
>
> show dbs
local  0.078GB

> use abccompany
switched to db abccompany

> db.getName()
abccompany

> show dbs
local  0.078GB

> db.stats()
{
    "db" : "abccompany",
    "collections" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "ok" : 1
}
>
> db.employees.insert({empId:24123,name:'Ramkumar',gender:'M',dept:'GMOT',location:'chennai',salaray:23500})
WriteResult({ "nInserted" : 1 })

> db.employees.insert({empId:24121,name:'Nagaraj',gender:'M',dept:'GRITT',location:'Mumbai',salaray:73500})
WriteResult({ "nInserted" : 1 })

> db.employees.insert({empId:24125,name:'Sandhya',gender:'F',dept:'GMOT',location:'NJ',salaray:1273500})
WriteResult({ "nInserted" : 1 })


> db.employees.insert({
... empId:24131,
... name:'Prakash',
... gender:'M',
... dateofbirth:new Date(1980,2,12,10,12),
... favmovies:['OKKanmani','JurasicWorld'],
... location:'NJ',
... salaray:33500})

WriteResult({ "nInserted" : 1 })

> db.employees.find({gender:'F'})

{ "_id" : ObjectId("5580fb46bec5eb9369552319"), "empId" : 24125, "name" : "Sandhya", "gender" : "F", "dept" : "GMOT", "location" : "NJ", "salaray" : 1273500 }
>

> db.employees.find({gender:'M', salaray:{$gt:50000}})

{ "_id" : ObjectId("5580fb03bec5eb9369552318"), "empId" : 24121, "name" : "Nagaraj", "gender" : "M", "dept" : "GRITT", "location" : "Mumbai", "salaray" : 73500 }

> db.employees.find( {gender:'M',     $or: [  {salaray:{$gt:50000}},   {dept:'GMOT'}]})

{ "_id" : ObjectId("5580fabebec5eb9369552317"), "empId" : 24123, "name" : "Ramkumar", "gender" : "M", "dept" : "GMOT", "location" : "chennai", "salaray" : 23500 }
{ "_id" : ObjectId("5580fb03bec5eb9369552318"), "empId" : 24121, "name" : "Nagaraj", "gender" : "M", "dept" : "GRITT", "location" : "Mumbai", "salaray" : 73500 }
>

> db.employees.find({"_id" : ObjectId("5580fb03bec5eb9369552318")})
{ "_id" : ObjectId("5580fb03bec5eb9369552318"), "empId" : 24121, "name" : "Nagaraj", "gender" : "M", "dept" : "GRITT", "location" : "Mumbai", "salaray" : 73500 }
>

> db.employees.update({name:"Nagaraj"}, {location: "NY"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>

> db.employees.find()
{ "_id" : ObjectId("5580fabebec5eb9369552317"), "empId" : 24123, "name" : "Ramkumar", "gender" : "M", "dept" : "GMOT", "location" : "chennai", "salaray" : 23500 }
{ "_id" : ObjectId("5580fb03bec5eb9369552318"), "location" : "NY" }
{ "_id" : ObjectId("5580fb46bec5eb9369552319"), "empId" : 24125, "name" : "Sandhya", "gender" : "F", "dept" : "GMOT", "location" : "NJ", "salaray" : 1273500 }
{ "_id" : ObjectId("5580fc1dbec5eb936955231a"), "empId" : 24131, "name" : "Prakash", "gender" : "M", "dateofbirth" : ISODate("1980-03-12T04:42:00Z"), "favmovies" : [ "OKKanmani", "JurasicWorld" ], "location" : "NJ", "salaray" : 33500 }
>

> db.employees.update({name: 'Ramkumar'}, {$set: {location:'London'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.employees.findOne()
{
    "_id" : ObjectId("5580fabebec5eb9369552317"),
    "empId" : 24123,
    "name" : "Ramkumar",
    "gender" : "M",
    "dept" : "GMOT",
    "location" : "London",
    "salaray" : 23500
}
>

> db.employees.remove({name: 'Ramkumar'})
WriteResult({ "nRemoved" : 1 })

> db.employees.remove({})
WriteResult({ "nRemoved" : 3 })

> db.employees.find()

> db.employees.drop()

No comments:

Post a Comment