You are on page 1of 6

CRUD

Adminstrative commands

#################################

Inserting documents in Mongo DB:

show dbs
use create_lesson_db

db # current context database

Search mongo db datafiles location

## inserting into sample collection


db.sample.insert({a:1})

db.sample.insert({a:3})
db.sample.insert({d:4})
db.sample.insert({b:2})
db.sample.insert({a:5})

db.sample.insert({a:4 , b:"a string" , someotherfield : 22.45})

db.sample.insert({"a" : 9})

db.sample.find()
db.sample.find().pretty()
var x = db.sample.find().toArray()
x

db.getLastError() ## checking last error


db.help()

db.temperature.insert({x:3,y:4})

#################################

Updates:

db.<collection_name>.update(<where>,<docs>)

two kinds of update in mongo:


1.) full update / replacement
2.) partial update

db.<collection_name>.update(<where>,<docs_or_partial_update_expression>)
[,<upsert> , <multi>]

upsert : update or insert if not present


multi : multiple entries

var t = db.sample
t.update({_id: 100} , {"_id" : 100 , x : "hello world" , y = 123})

myobj = t.findOne()
myobj
myobj.y=123
myobj

t.update({_id : myobj._id}, 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} , {$set: { y : 100 }}) ## setting value of y equal to 100

JSON RFC ## Read about it

t.update({ _id : 101} , {$inc : { y : 1 }}) ##incrementing y by 1

t.update({ _id : 101} , {$push : { arr : "hi"}}) ## push an item into array

######################################
Removing documents:

db.<collection_name>.remove(<expression>)

db.test.find({ _id : 100 })


db.test.remove ({_id : 100})

db.test.remove ({}) ## will remove all documents from collection

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)

db.test2.remove({ x : /ello/ }) ##using regular expression

///

db.users.remove({ "addr.city" : "Lyon" , registered : false})

##################################
Multi doc Update::

db.<coll>.update (<where>, <obj>,<upsert>,<multi>)


false false
db.users.update({active : true} , {$inc : {priority : 1}} , false , true)

##################################

UPSERTS::

update , or insert , if not present

db.pageviews.upadte({_id: "/sports/football"}, {$inc : {views : 1}}, true)


_____

db.users.update({_id : "Jane"} , {$addToSet : {likes:"football"}}, {upsert : true})

##################################

Wire Protocol :

mongo/BSON wire protocol : query , insert , update , remove , getmore

client >>>> query >>>> server >>>>query responce to Client


overloading the query operator in wire protocol

special command .$cmd


##################################

Bulk write operations:

two basic forms of :


1.) ordered (db.items.initializeOrderedBulkOp();)
2.) Unordered ( more efficient)

var bulk = db.items.initializeUnorderedBulkOp();

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

admin : special reserve database name

docs.mongodb.org/manual/reference/commands

##################################
db.run commands ::

db.runCommand({})

db.runCommand({ <commmand>: <value>})

db.runCommand({getLastError : 1 , w : 3 , wtimeout : 10})

##################################
db.isMaster :

ensureIndex
dropIndex
currentOp
killOp

#to check the server is master or slave

# to determine if this member of replia set is primary


db.runCommand({isMaster : 1 })

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
##################################

db.currentOp() & db.killOp()

##################################
collection.stats() & collection.drop()

db.test.stats()
db.test.drop()

INDEXES ARE IN B-TRESS in MONGODB

db.system.namespaces.find()

## mongo db collections are created implicitly

##################################

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

_id index already created


index limit is 40

##################################

mongoimport --drop -d pcat -c products Products.json

db.products.find({_id : ObjectId("507d95d5719dbef170f15c00")}).update({_id :
ObjectId("507d95d5719dbef170f15c00")}, {$set: { "term_years" : 3 }})

db.products.update({_id : ObjectId("507d95d5719dbef170f15c00")} , {$set :


{ "limits.sms.over_rate" : 0.01 }})

db.products.find({ "limits.voice": { "$exists" : true }}).count()

db.products.update({_id : ObjectId("507d95d5719dbef170f15c00")} , {$inc :


{ "term_years" : 1 }})

var x = db.products
x.update({_id : ObjectId("507d95d5719dbef170f15c00")}, {$set : { "term_years" :
3 }})

x.find({_id : ObjectId("507d95d5719dbef170f15c00"}).count()

You might also like