Indexes
Every MongoDB index type. Each index is global and is updated in the same write as the document.
TeselaDB has every MongoDB index type: single field, compound, multikey, unique, partial, sparse, hidden, TTL, with
collation, hashed, wildcard, 2dsphere and 2d. Text indexes are in Search.
Requirements
- A user with
dbAdminorreadWriteon the database to create indexes.
Create indexes
db.orders.createIndex({ customer: 1, created: -1 });
db.users.createIndex({ email: 1 }, { unique: true });
db.sessions.createIndex({ lastSeen: 1 }, { expireAfterSeconds: 3600 });Global indexes
Every index is global: its entries are kept sorted in the same key space as the documents and are written in the same operation. If the document and its index entry fall in the same range, the write is single-phase. If not, it goes in a transaction. You never see an index that does not match its documents.
Uniqueness across the cluster
A unique index is unique across the whole cluster, also in a collection with a shard key and without that key as a prefix. MongoDB only guarantees uniqueness within each shard.
Increasing keys
An index on a value that always grows (a date, a counter) concentrates writes on the last range, because each
new entry goes after the previous one in the order of the index. TeselaDB's own option
teselaBuckets spreads its entries over hash buckets; the planner merges the buckets on read:
db.events.createIndex({ created: 1 }, { teselaBuckets: 8 });Online builds
createIndexes builds the index without stopping the writes to the collection, and collMod changes hidden,
expireAfterSeconds or unique on an existing index.
Check
db.orders.getIndexes() lists the new index. The explain of a query on its fields shows that it uses it.
Common problems
- Writes pile up on a single range because the index is on a value that always grows. Create it with
teselaBuckets. - A unique index is not created because the collection already has documents with repeated values in those fields.