Professional Documents
Culture Documents
Chapter 2
Chapter 2
Adminstrative commands
#################################
show dbs
use create_lesson_db
db.sample.insert({a:3})
db.sample.insert({d:4})
db.sample.insert({b:2})
db.sample.insert({a:5})
db.sample.insert({"a" : 9})
db.sample.find()
db.sample.find().pretty()
var x = db.sample.find().toArray()
x
db.temperature.insert({x:3,y:4})
#################################
Updates:
db.<collection_name>.update(<where>,<docs>)
db.<collection_name>.update(<where>,<docs_or_partial_update_expression>)
[,<upsert> , <multi>]
var t = db.sample
t.update({_id: 100} , {"_id" : 100 , x : "hello world" , y = 123})
myobj = t.findOne()
myobj
myobj.y=123
myobj
###################################
save command:
myobj.y = 400
t.save(myobj)
###################################
Partial Updates:
$set
$push
$unset
$pop
$addToSet ## it adds the value to set if it doesnot exist, if it already exists
does not append it
t.update({ _id : 101} , {$push : { arr : "hi"}}) ## push an item into array
######################################
Removing documents:
db.<collection_name>.remove(<expression>)
just copying the old output for json and paste it in a new file with .json
extension
then issue the commane ( mongoimport -d pcat - c test2 test.json)
///
##################################
Multi doc Update::
##################################
UPSERTS::
##################################
Wire Protocol :
db.items.find()
bulk.insert({item : "abc123" , defaultQty : 100 , staus : "A" ,points : 100})
db.items.find() ## still no items inserted
bulk.execute() ## it will upload the data in server
##################################
getLastError
isMaster
drop
creatre
compact
serverStatus
replSetGetStatus
addShard
aggregate
MapReduce
Count
findAndModify
docs.mongodb.org/manual/reference/commands
##################################
db.run commands ::
db.runCommand({})
##################################
db.isMaster :
ensureIndex
dropIndex
currentOp
killOp
db.runCommand("isMaster")
db.isMaster()
db.isMaster
##################################
db.serverStatus() used by mms
when running the command, it returns "OK" in the end: it represents that the
command was sent and recieved properly by mongodb
##################################
##################################
collection.stats() & collection.drop()
db.test.stats()
db.test.drop()
db.system.namespaces.find()
##################################
server:
isMaster
serverstatus
logout
getLastError
db:
dropDatabase
repairDatabase
clone
copydb
dbstats
index:
ensureIndex
dropIndex
collection:
create
drop
collstats
renameColltion
------------
count
aggregate
MapReduce
FindAndModify
geo* spatial index
##################################
db.products.find({_id : ObjectId("507d95d5719dbef170f15c00")}).update({_id :
ObjectId("507d95d5719dbef170f15c00")}, {$set: { "term_years" : 3 }})
var x = db.products
x.update({_id : ObjectId("507d95d5719dbef170f15c00")}, {$set : { "term_years" :
3 }})
x.find({_id : ObjectId("507d95d5719dbef170f15c00"}).count()