Guide · Open source
How to use NeDB: install, create a datastore and query data
Updated · written with AI from the project's own documentation and checked against our catalogue data
NeDB is an embedded database library written entirely in JavaScript. You add it to a Node.js, nw.js, Electron or browser project, and it can keep data purely in memory or save it to a file on disk. This guide shows you how to install it, create a datastore, run persistence and compaction, and decide whether it fits your project.
Quick start
- 1Install the package. Run npm install nedb --save in your Node.js project so the library is listed in package.json.
- 2Choose a storage mode. Create an in-memory datastore by omitting filename, or a persistent one by passing a filename.
- 3Load the datastore. Set autoload to true for automatic loading, or call loadDatabase before issuing commands.
- 4Insert documents. Add documents to the datastore using the insert call from the README's inserting documents section.
- 5Query and update. Use MongoDB-style find, update and remove operations, with operators like $gt or $in.
- 6Maintain the data file. Let automatic compaction on load handle the file, or call compactDatafile manually when needed.
Install nedb with npm
nedb is installed from npm. The module name on npm and bower is nedb. The prerequisite listed for this project is Node.js.
- Add the library to your project and save it to package.json.
- Run the test suite if you want to check the library itself; this needs the dev dependencies installed.
npm install nedb --save # Put latest version in your package.json
npm test # You'll need the dev dependencies to launch testsCreate an in-memory or persistent datastore
You can use NeDB as an in-memory only datastore or as a persistent datastore. One datastore is the equivalent of a MongoDB collection. You create one with new Datastore(options). If you omit filename, the datastore is in-memory only and does not need loading.
For a persistent datastore, set filename to the path of the data file. Set autoload to true to load the file automatically on creation. Without autoload, you must call loadDatabase yourself before running commands, because no insert, find, update or remove runs before the load finishes.
- filename (optional): path to the file where data is persisted. It cannot end with a ~.
- inMemoryOnly (optional, defaults to false): as the name implies.
- timestampData (optional, defaults to false): adds createdAt and updatedAt fields to documents.
- autoload (optional, defaults to false): loads the database upon creation.
- corruptAlertThreshold (optional): between 0 and 1, defaults to 10%. NeDB refuses to start if more than this share of the file is corrupt.
var Datastore = require('nedb')
, db = new Datastore({ filename: 'path/to/datafile', autoload: true });
// You can issue commands right awayUnderstand persistence and compaction
NeDB uses an append-only format. Updates and deletes add lines to the end of the data file for performance. The database is automatically compacted, meaning put back into one line per document, every time you load it.
You can compact manually with yourDatabase.persistence.compactDatafile, which takes no argument and queues the compaction after pending operations. You can also set automatic compaction with setAutocompactionInterval(interval), where the interval is in milliseconds and a minimum of 5s is enforced. The README notes that compaction blocks other operations, so most projects do not need it.
- Compaction took about 130ms for 50k records on a typical development machine, according to the README.
- The datastore fires a compaction.done event when compaction finishes.
- Compaction also removes documents whose data line is corrupted, as long as corruption stays below corruptAlertThreshold.
yourDatabase.persistence.compactDatafile();
yourDatabase.persistence.setAutocompactionInterval(interval);
yourDatabase.persistence.stopAutocompaction();Query data with a MongoDB-style API
The API is a subset of MongoDB's, covering the most used operations. The README lists support for creating and loading databases, inserting, finding, counting, updating, removing and indexing documents. Find queries support operators such as $lt, $lte, $gt, $gte, $in, $nin, $ne, $exists and $regex, along with logical operators $or, $and, $not and $where.
Queries also support sorting and paginating, projections, and array fields. The full details are in the README sections on these topics.
- Inserting documents
- Finding documents with basic querying and operators
- Sorting and paginating
- Counting, updating, and removing documents
- Indexing
Use serialization hooks with care
The afterSerialization and beforeDeserialization options let you transform data before it is written to disk, for example to encrypt it. The README says the transformed string must not contain a newline character, and that the two hooks must be inverses of one another. Declare both or you risk data loss.
NeDB checks that both hooks are declared together and tests that they reverse each other on random strings. If too much data looks corrupt, it refuses to start.
Know the limits before you commit
The README states that the library is no longer maintained and may have bugs and security issues. It says no pull request or security alert will be answered, and suggests forking if you need changes. The README also stresses small embedded use, and compaction blocks other operations, so large datasets and heavy concurrent workloads are a poor fit.
What people use it for
- Storing settings and small records in an Electron desktop app without installing a database server.
- Using an in-memory datastore in tests or prototypes.
- Keeping a lightweight local store in a Node.js tool for developers already familiar with MongoDB queries.
- Running a small project where avoiding binary dependencies matters.
Frequently asked questions
Is nedb still maintained?
No. The README warns that the library is no longer maintained, may have bugs and security issues, and that no pull request or security alert will be answered. You can fork it, but treat it as a risk for production use.
How do I install nedb?
Run npm install nedb --save in a Node.js project. The README notes that the module name on npm and bower is nedb.
Does nedb store data in memory or on disk?
Both. Omit filename for an in-memory only datastore, or set filename to a path to persist data to a file on disk.
Is nedb compatible with MongoDB?
Only partly. Its API is described as a subset of MongoDB's, covering the most used operations, so not every MongoDB feature is available.
What are alternatives to nedb?
Projects listed as comparable are LowDB, PouchDB, Dexie.js, and SQLite via better-sqlite3. Choose based on whether you need a browser store, sync, or a relational database.
Why does my nedb datastore not run commands?
For a persistent datastore without autoload, no command runs until loadDatabase is called. Call it yourself or set autoload to true.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
NeDB suits small Node.js or Electron apps, prototypes and tests that need simple local storage with MongoDB-style queries and no separate server. Pick something else, such as SQLite via better-sqlite3 or an actively maintained option, for production systems that need security fixes or for large datasets and heavy concurrent work.
Install options & downloads