Skip to content

Instantly share code, notes, and snippets.

@hassansin
Last active September 3, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hassansin/93654867fc74c9215c92 to your computer and use it in GitHub Desktop.
Save hassansin/93654867fc74c9215c92 to your computer and use it in GitHub Desktop.
MongoDB commands
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: 'test@sendgrid.com' }).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: 'test@sendgrid.com' }) // 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