Show Menu
Cheatography

MongoDB Cheat Sheet by

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­('o­ld_­name', 'new_n­ame');
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>
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.