Last active
September 3, 2020 16:35
-
-
Save hassansin/93654867fc74c9215c92 to your computer and use it in GitHub Desktop.
MongoDB commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.collection.createIndex( { orderDate: 1, zipcode: -1 }, {background: true} ) | |
db.events.ensureIndex( { "timestampISO": 1 }, { expireAfterSeconds: 120*24*60*60 } ) // <3.0, TTL index, 120day retention period | |
db.collection.getIndexes() //get all indexes | |
db.collection.dropIndex("name"); //drop single index | |
db.collection.dropIndexes(); //drop all indexes | |
db.collection.find({ email: '[email protected]' }).explain("executionStats") // <3.0 : https://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explain | |
//https://docs.mongodb.org/manual/tutorial/measure-index-use/ | |
db.collection.explain("executionStats").find({ email: '[email protected]' }) // 3.0 + | |
db.events.totalIndexSize() // in bytes, should not exceed RAM | |
/* | |
- Analyze application queries | |
- Find the queries that'll run most | |
- Consider the relative frequency of each query in the application and whether the query justifies an index. | |
- Profile a variety of index configurations with data sets | |
- Indexes are stored in RAM, make sure indexSize doesn't exceed RAM - will cause performance hit otherwise | |
*/ | |
db.events.find({ "timestampISO" : { $type : 2 } } ).snapshot().forEach(function(doc){ | |
db.events.update({_id: doc._id}, { | |
$set: | |
{ | |
timestampISO: new Date(doc.timestamp) | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment