Registry / database / react-native-local-mongodb

react-native-local-mongodb

JSON →
library4.0.0jsnpmunverified

A lightweight embedded persistent or in-memory database for React Native, providing a subset of MongoDB's API (NeDB-compatible). Current stable version is 4.0.0. It uses AsyncStorage for persistence on React Native. Key differentiators: no native dependencies, TypeScript types included, supports indexing, serialization hooks, and timestamp data. Compared to alternatives like WatermelonDB or Realm, it is simpler and ideal for small offline-first apps. The library switched from promise-based methods to callback-style in v2.0.0, adding Async methods.

npm install react-native-local-mongodb
INSTALL
IMPORT
SIG · REACT-NATIVE-LOCAL
R
react-native-local-mongodb
databasejavascriptv4.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Datastore
✓ import { Datastore } from 'react-native-local-mongodb';
✗ const Datastore = require('react-native-local-mongodb');
Default export is Datastore, which is a class. The 'async' suffix methods (e.g., insertAsync) are available since v2. Common mistake: using require() but package is ESM-friendly with TypeScript types.
Datastore
✓ const { Datastore } = require('react-native-local-mongodb');
✗ const Datastore = require('react-native-local-mongodb').Datastore;
For CommonJS usage, destructure from the module. Do not access .Datastore property incorrectly.
✓ import type { Datastore } from 'react-native-local-mongodb';
TypeScript users: import the type if only need type information, but the library ships types so no separate type package needed.

Create a persistent Datastore, insert a document using both callback and Async methods, and perform a simple find with sorting.

import { Datastore } from 'react-native-local-mongodb'; // Create a database collection (persistent) const db = new Datastore({ filename: 'mycollection', autoload: true }); // Insert a document using callback (pre-Async style) db.insert({ hello: 'world', count: 1 }, (err, newDoc) => { if (err) console.error(err); else console.log('Inserted:', newDoc); }); // Or use insertAsync (promise-based) db.insertAsync({ hello: 'world', count: 2 }) .then(doc => console.log('Async inserted:', doc)) .catch(err => console.error(err)); // Find documents db.find({ hello: 'world' }, (err, docs) => { console.log('Found:', docs); }); // Find with sort and limit db.find({}).sort({ count: -1 }).limit(2).exec((err, docs) => { console.log('Top two:', docs); });
Debug
Known issues
breakingIn v2.0.0, insert, update, remove, find, and findOne no longer return promises. Use the Async suffix methods (insertAsync, etc.) for promise support.
fix
Use callback syntax or switch to Async methods (e.g., insertAsync) for promises.
affects: >=2.0.0
deprecatedThe in-memory only mode is still supported but the filename option is ignored in that case. Ensure you don't rely on persistence when inMemoryOnly is true.
fix
If you need persistence, set inMemoryOnly to false and provide a filename.
affects: *
gotchaPersistence uses AsyncStorage keys that may be cleared by the user or during app updates. Data is not guaranteed to survive across reinstalls.
fix
Consider using a more robust solution like WatermelonDB if data durability is critical.
affects: *
gotchaAfterSerialization and beforeDeserialization hooks must be inverses; the library checks randomly but misuse can cause data loss.
fix
Ensure your hooks are exact inverses and test with various string lengths.
affects: *
Errors
Common errors & fixes
Cannot read property 'insert' of undefined
Trying to use db.insert before the Datastore is fully loaded (async).
fix
Use autoload: true or load the database via loadDatabase callback before performing operations.
db.insertAsync is not a function
Using an older version (<2.0.0) where Async methods didn't exist.
fix
Update to v2.0.0+ or use callback-based insert.
You can't use inMemoryOnly mode with a filename
Passing both filename and inMemoryOnly: true is not allowed.
fix
If you want in-memory, omit filename. If you want persistence, set inMemoryOnly: false.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
react-nativerequiredRequired as a peer dependency for React Native environment.
Agent activity
2 hits · last 30 days
node
2
Resources
react-native-local-mongodb — npm install react-native-local-mongodb · libregistry