Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
Last active August 29, 2015 14:03
Show Gist options
  • Save saintc0d3r/243c27ff85bb7dbcc86e to your computer and use it in GitHub Desktop.
Save saintc0d3r/243c27ff85bb7dbcc86e to your computer and use it in GitHub Desktop.
Add indexes on a collection's columns in mongodb
// Let the collection's name is 'students' & we want to speed up the query's performance on the 'students' collection when we find documents in the collection by 'student_id'.
// In order to achieve this, we'll add an index for the 'student_id' column where we want to index it by ascending order.
db.students.ensureIndex({'student_id': 1})
// Another case is when we want to speed up query which takes both 'student_id' & 'class' columns where the class would be descending order.
db.students.ensureIndex({'student_id':1, 'class':-1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment