Update README.md

pull/2/head
Louis Chatriot 11 years ago
parent 1e3be62b56
commit 119948aee7
  1. 7
      README.md

@ -446,6 +446,9 @@ To create an index, use `datastore.ensureIndex(options, cb)`, where callback is
Note: the `_id` is automatically indexed with a unique constraint, no need to call `ensureIndex` on it. 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.
```javascript ```javascript
db.ensureIndex({ fieldName: 'somefield' }, function (err) { db.ensureIndex({ fieldName: 'somefield' }, function (err) {
@ -470,6 +473,10 @@ db.insert({ somefield: 'nedb' }, function (err) {
// , message: 'Unique constraint violated for key name' } // , message: 'Unique constraint violated for key name' }
}); });
}); });
// Remove index on field somefield
db.removeIndex('somefield', function (err) {
});
``` ```
**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). **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).

Loading…
Cancel
Save