Indexing
NeDB supports indexing. It gives a very nice speed boost and can be used to enforce a unique constraint on a field. You can index any field, including fields in nested documents using the dot notation. For now, indexes are only used to speed up basic queries and queries using $in
, $lt
, $lte
, $gt
and $gte
. The indexed values cannot be of type array of object.
To create an index, use datastore.ensureIndex(options, cb)
, where callback is optional and get passed an error if any (usually a unique constraint that was violated). ensureIndex
can be called when you want, even after some data was inserted, though it's best to call it at application startup. The options are:
- fieldName (required): name of the field to index. Use the dot notation to index a field in a nested document.
- unique (optional, defaults to
false
): enforce field uniqueness. Note that a unique index will raise an error if you try to index two documents for which the field is not defined. - sparse (optional, defaults to
false
): don't index documents for which the field is not defined. Use this option along with "unique" if you want to accept multiple documents for which it is not defined. - expireAfterSeconds (number of seconds, optional): if set, the created index is a TTL (time to live) index, that will automatically remove documents when the system date becomes larger than the date on the indexed field plus
expireAfterSeconds
. Documents where the indexed field is not specified or not aDate
object are ignored
Note: the _id
is automatically indexed with a unique constraint, no need to call ensureIndex
on it.
You can remove a previously created index with datastore.removeIndex(fieldName, cb)
.
If your datastore is persistent, the indexes you created are persisted in the datafile, when you load the database a second time they are automatically created for you. No need to remove any ensureIndex
though, if it is called on a database that already has the index, nothing happens.
db.ensureIndex({ fieldName: 'somefield' }, function (err) {
// If there was an error, err is not null
});
// Using a unique constraint with the index
db.ensureIndex({ fieldName: 'somefield', unique: true }, function (err) {
});
// Using a sparse unique index
db.ensureIndex({ fieldName: 'somefield', unique: true, sparse: true }, function (err) {
});
// Format of the error message when the unique constraint is not met
db.insert({ somefield: 'nedb' }, function (err) {
// err is null
db.insert({ somefield: 'nedb' }, function (err) {
// err is { errorType: 'uniqueViolated'
// , key: 'name'
// , message: 'Unique constraint violated for key name' }
});
});
// Remove index on field somefield
db.removeIndex('somefield', function (err) {
});
// Example of using expireAfterSeconds to remove documents 1 hour
// after their creation (db's timestampData option is true here)
db.ensureIndex({ fieldName: 'createdAt', expireAfterSeconds: 3600 }, function (err) {
});
// You can also use the option to set an expiration date like so
db.ensureIndex({ fieldName: 'expirationDate', expireAfterSeconds: 0 }, function (err) {
// Now all documents will expire when system time reaches the date in their
// expirationDate field
});
Note: the ensureIndex
function creates the index synchronously, so it's best to use it at application startup. It's quite fast so it doesn't increase startup time much (35 ms for a collection containing 10,000 documents).