Sorting, Indexing & Searching
Sorting Records
// Sort documents in ascending order by the "age" field
db.collectionName.find().sort({ age: 1 })
// Sort documents in descending order by the "name" field
db.collectionName.find().sort({ name: -1 })
// Sort by multiple fields (first by "age" ascending, then by "name" descending)
db.collectionName.find().sort({ age: 1, name: -1 })Creating and Deleting Text Indexes
db.collectionName.createIndex({ fieldName: "text" })db.collectionName.dropIndex({ fieldName: "text" })Text Search on a Collection
Last updated