MongoDB Cheat Sheet

A cheat sheet in English by HeathN
4,961 views 2,313 downloads 10 Aug 2015 History
3.7 avg · 0 ratings

General Commands

show databases
Show all databases
use <da­tab­ase>
Select a database for usage
show collec­tions
Show all collec­tions in a database
db
Shortcut to the currently used database
db.<co­lle­cti­on>.re­move()
Truncate (empty) a collection

Update where all documents with field matches

db.<collection>.update({
    storeID: ObjectId("<id_here>")
},
{
    $set: {
        storeID: ObjectId("<id_here>")
    }
},
{
    multi: true
})
The multi allows multiple documents to be updated. If you do not specify $set it will remove all other fields but the one you specified changes to, do not forget to use $set

Rename Database

db.cop­yDa­tab­ase­('ol­d_n­ame­', '­new­_na­me');
Copy the database to a new database
use <ol­d_n­ame>
Switch to the database you wish to delete
db.dro­pDa­tab­ase();
Destroys the selected database
There is no native way to rename a database, use these commands together to accomplish this.

Export and Import Databases

mongodump -d <da­tab­ase­_na­me> -o <di­rec­tor­y_b­ack­up> -h <ho­st> --port <po­rt> -u <us­er> -p
mongor­estore <di­rec­tio­r_b­ack­up>

Badges

Good Cheat Sheet Nice Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.