In general, MongoDB can use a compound index to return sorted results if:
{ a: 1, b: 1, c: 1, d: 1 }
and which query search and sort use index to improve performance? and why?
db.collection.find().sort( { a:1 } ) db.collection.find().sort( { a:1, b:1 } ) db.collection.find( { a:4 } ).sort( { a:1, b:1 } ) db.collection.find( { b:5 } ).sort( { a:1, b:1 } ) db.collection.find( { a:{ $gt:4 } } ).sort( { a:1, b:1 } ) db.collection.find( { a:5 } ).sort( { a:1, b:1 } ) db.collection.find( { a:5 } ).sort( { b:1, c:1 } ) db.collection.find( { a:5, c:4, b:3 } ).sort( { d:1 } ) db.collection.find( { a:5, b:3, d:{ $gt:4 } } ).sort( { c:1 } ) db.collection.find( { a:5, b:3, c:{ $lt:2 }, d:{ $gt:4 } } ).sort( { c:1 } )
db.collection.find().sort( { b:1 } ) db.collection.find( { b:5 } ).sort( { b:1 } ) db.collection.find( { b:{ $gt:5 } } ).sort( { a:1, b:1 } )
Thanks so much!!
Phuc,
Cheers...
db.collection.find().sort( { b:1 } )
db.collection.find( { b:5 } ).sort( { b:1 } )db.collection.find().sort( { a:1 } ).explain()db.collection.getIndexes()