NeDB is not intended to be a replacement of large-scale databases such as MongoDB! Its goal is to provide you with a clean and easy way to query data and persist it to disk, for web applications that do not need lots of concurrent connections, for example a <ahref="https://github.com/louischatriot/braindead-ci"target="_blank">continuous integration and deployment server</a> and desktop applications built with <ahref="https://github.com/rogerwang/node-webkit"target="_blank">Node Webkit</a>.
I recently benchmarked NeDB against the popular client-side database <ahref="http://www.taffydb.com/"target="_blank">TaffyDB</a> and <ahref="https://github.com/louischatriot/taffydb-benchmark"target="_blank">NeDB is much, much faster</a>, so I'm considering porting it to browsers. Please comment on <ahref="https://github.com/louischatriot/nedb/issues/23">this issue</a> if you want it.
It's a subset of MongoDB's API (the most used operations). The current API will not change, but I will add operations as they are needed. Summary of the API:
*<ahref="#creatingloading-a-database">Creating/loading a database</a>
You can use NeDB as an in-memory only datastore or as a persistent datastore. One datastore is the equivalent of a MongoDB collection. The constructor is used as follows `new Datastore(options)` where `options` is an object with the following fields:
*`filename` (optional): path to the file where the data is persisted. If left blank, the datastore is automatically considered in-memory only.
*`inMemoryOnly` (optional, defaults to false): as the name implies.
*`pipeline` (optional, defaults to false): use pipelining. This is an experimental feature that speeds up writes (about 2x) but can sometime increase read times. You probably shouldn't use it yet.
An `_id` field will be automatically generated by NeDB. It's a 16-characters alphanumerical string that cannot be modified once it has been generated. Unlike with MongoDB, you cannot specify it (that shouldn't be a problem anyway).
Use `find` to look for multiple documents matching you query, or `findOne` to look for one specific document. You can select documents based on field equality or use comparison operators (`$lt`, `$lte`, `$gt`, `$gte`, `$in`, `$nin`, `$ne`). You can also use logical operators `$or`, `$and` and `$not`. See below for the syntax.
*`update` specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!)
* The available modifiers are `$set` to change a field's value, `$inc` to increment a field's value and `$push`, `$pop`, `$addToSet` to work on arrays. The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. See examples below for the syntax
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`.
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.
**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).
NeDB is not intended to be a replacement of large-scale databases such as MongoDB, and as such was not designed for speed. That said, it is still pretty fast on the expected datasets, especially if you use indexing. On my machine (3 years old, no SSD), with a collection containing 10,000 documents, with indexing and no pipelining:
* If you've outgrown NeDB, switching to MongoDB won't be too hard as it is the same API. Use <ahref="https://github.com/louischatriot/nedb-to-mongodb"target="_blank">this utility</a> to transfer the data from a NeDB database to a MongoDB collection