MongoDB shell cheatsheet
Post date: Jan 06, 2015 4:21:56 PM
MongoDB cheatsheet for Illiad management, adapted from [1]
Insert documents
db.collection.insert({name:'USS',crew:50,codes:[10,17,19]})
Find documents
Find one document: db.collection.findOne()
Find document: db.collection.find()
Find document and display partial field : db.collection.find({}, {name:true, _id:false})
Find document with criterion: db.collection.find({crew:{$gt:10}})
Find document, sort descending, and return limited results: db.collection.find().sort({crew:-1}).limit(2)
Update documents
Replace the entire document: db.collection.update({name : 'USS'}, {name : 'USAF'})
Set / change certain attributes of a given document: db.collection.update({name : 'USS'}, {$set : {operator : 'Starfleet', class : 'Prometheus'}})
Remove an attribute from a given document: db.collection.update({name : 'USS'}, {$unset : {operator : 1}})
Remove documents
db.collection.remove({name : 'USS'})
Indexes
Creating an index in ascending order: db.collection.ensureIndex({name : 1})
Dropping an index by name: db.collection.dropIndex('name_1')
Remove a collection
db.collection.drop()
Collection statistics
db.collection.count()
Controls
Clear screen: CTLR+L
[1] https://blog.codecentric.de/files/2012/12/MongoDB-CheatSheet-v1_0.pdf