Hassle-free persistence with Node

NeDB is an in-memory database with persistence possibility implemented entirely in javascript. Its NoSQL architecture is oriented to documents that are stored in JSON format.


We will see the steps needed to install, set up and use the database in a Node.js application.


To install it, we simply need to be positioned in the root directory of our application and run the command.


The flag-save is used to add NeDB as a dependency of the application.
With this command alone, the nedb is ready for use, there is no need to install an extra database and then use a connector as it would be necessary with, for example, a MongoDB database.

Creating the collection


The first line is needed to import the module NeDB and the second one is the creation of a collection itself. The filename when creating the collection is optional; if NeDB is not present, it interprets that the collection will be used in memory directly. When files are used, they are automatically created and all folders that are specified in its path as well.

Insert documents in the collection


These changes will automatically persist in the file associated with the collection.

Frequent queries

To query, the find ( query, function(error, result) ) method is used, which receives as its first parameter a query that is used to retrieve documents that match the search criteria. The result of this operation is the result of the function parameter that accompanies the query. The parameter name is arbitrary, but the first one contains the error message and the second one the results, always.
If the query is left empty, it returns all the documents, and if there are no documents that match the query, it results in an empty array. Here you can see some examples of queries:

All users


Filter a user field value against a regular expression


User filter value of a field with relational operators


Delete documents

To delete documents, use the remove(query, [ { multi: true } ], function (error, numRemoved)) method, the first parameter is a query can be just like the find method. The second parameter is optional, and it indicates the “remove” applies to all results of the query, or only one; if omitted, it is taken as false by default, if multi: false and the query returns more than one document, the first one is removed. The last parameter is a callback function that contains the error message (if there are any) and the number of documents removed. If there is an error, numRemoved === null.

Delete a document


Delete all documents


Update documents

To update documents, the update( query, updateMode, [ { multi: true} ], function(error, numUpdated) )method is used. The first parameter can be a query, just like in the above two methods, and the update will be applied on its result. updateMode specifies which field will be updated and how; for example you can change the value of a field, inserting a new field or replacing all fields in a document with new ones, with certain values. The third parameter is a function that, just like the elimination method, contains an error and number of documents affected. The optional parameter multi on the same way as the remove one.

Update a document


Update more than one document at a time


Extra material and references

Sample application

This repository is an application that implements NeDB Node.js to see everything explained here in action.

NeDB repository

In the NeDB repository, one can find its detailed information with usage examples for each of its methods.

Share this articleShare on LinkedInTweet about this on TwitterShare on FacebookEmail this to someone
Go Back