Mongodb - Cheat Sheet Page 2

ADVERTISEMENT

More information can be found from the MongoDB Tutorial
MongoDB - Cheat Sheet
@
th
Version 1.0 / 27
December 2012 / Page 2 of 4
Updating Documents
Replaces the whole document
db.ships.update({name : 'USS Prometheus'}, {name : 'USS Something'})
sets / changes certain attributes
db.ships.update({name : 'USS Something'},
{$set : {operator : 'Starfleet', class : 'Prometheus'}})
of a given document
db.ships.update({name : 'USS Something'},
removes an attribute from a
{$unset : {operator : 1}})
given document
Removing Documents
db.ships.remove({name : 'USS Prometheus'})
removes the document
removes using operator
db.ships.remove({name:{$regex:’^USS\\sE’}})
Each individual document removal is atomic with respect to a concurrent reader or writer. No client will see a document half
G+ Community Page:
removed.
115421122548465808444
Working with Indexes
Creating an index
db.ships.ensureIndex({name : 1})
Dropping an index
db.ships.dropIndex({name : 1})
Creating a compound index
db.ships.ensureIndex({name : 1, operator : 1, class : 0})
Dropping a compound index
db.ships.dropIndex({name : 1, operator : 1, class : 0})
Creating a unique compound index
db.ships.ensureIndex({name : 1, operator : 1, class : 0}, {unique : true})
Indexes – Hints & Stats
Top & Stats System Commands
Explains index usage
Shows time spent per operations per collection
db.ships.find ({'name':'USS Defiant'}).explain()
./mongotop
Index statistics
Shows snapshot on the MongoDB system
db.ships.stats()
./mongostat
Index size
db.ships.totalIndexSize()

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4