Mongo - Performance
Lesson Objectives
- Explain indexes
- Explain text indexes
- Explain replication
- Explain sharding
Explain indexes
db.employees.ensureIndex({name: 1});db.employees.dropIndex({name: 1});db.employees.ensureIndex({name: 1}, {unique: true});- indexes on embedded fields
- indexes on arrays
db.employees.ensureIndex({name: 1, salary: -1});
Explain text indexes
db.articles.createIndex({ subject : "text", content : "text" })- drop text index
db.articles.getIndexes()db.articles.dropIndex('index_name')
db.articles.find( { $text: { $search: "coffee" } } )db.articles.find( { $text: { $search: "leche", $language: "es" } } )
- stop words (a, the, and, etc)
db.articles.find( { $text: { $search: "bake coffee cake" } } )db.articles.find( { $text: { $search: "\"coffee cake\"" } } )db.articles.find( { $text: { $search: "bake coffee -cake" } } )db.articles.find( { $text: { $search: "cake" } }, { score: { $meta: "textScore" } } )
db.articles
.find({ $text: { $search: "cake" } }, { score: { $meta: "textScore" } })
.sort({ score: { $meta: "textScore" } });
Explain replication
- Writes sent to secondary DBs asynchonously
- Reads can happen on secondaries
- although data may be stale
- If primary goes down, a secondary is chosen to be new primary
Explain sharding
- split data across multiple servers