Embedded persistent or in memory database for react-native. API is a subset of MongoDB's (NeDB).
Installation, tests
Module name on npm is react-native-local-mongodb
.
Starting with version 3.0.0 we use @react-native-community/async-storage
as a peer pependence
Starting with version 2.0.0, the insert
, update
, remove
, find
, and findOne
methods no longer return promises - now they have a corresponding Async
method (insertAsync, updateAsync, etc )
npm install react-native-local-mongodb --save # Put latest version in your package.json
npm test # You'll need the dev dependencies to launch tests
API
It is a subset of MongoDB's (NeDB) API (the most used operations).
- Creating/loading a database
- Persistence
- Inserting documents
- Finding documents
- Basic Querying
- Operators ($lt, $lte, $gt, $gte, $in, $nin, $ne, $exists, $regex)
- Array fields
- Logical operators $or, $and, $not, $where
- Sorting and paginating
- Projections
- Counting documents
- Updating documents
- Removing documents
- Indexing
- Browser version
Creating/loading a database
You can use react-native-local-mongodb 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): the key where AsyncStorage will save data.
inMemoryOnly
(optional, defaults to false
): as the name implies.
timestampData
(optional, defaults to false
): timestamp the insertion and last update of all documents, with the fields createdAt
and updatedAt
. User-specified values override automatic generation, usually useful for testing.
autoload
(optional, defaults to false
): if used, the database will automatically be loaded from the datafile upon creation (you don't need to call loadDatabase
). Any command issued before load is finished is buffered and will be executed when load is done.
onload
(optional): if you use autoloading, this is the handler called after the loadDatabase
. It takes one error
argument. If you use autoloading without specifying this handler, and an error happens during load, an error will be thrown.
afterSerialization
(optional): hook you can use to transform data after it was serialized and before it is written to disk. Can be used for example to encrypt data before writing database to disk. This function takes a string as parameter (one line of an react-native-local-mongodb data file) and outputs the transformed string, which must absolutely not contain a \n
character (or data will be lost).
beforeDeserialization
(optional): inverse of afterSerialization
. Make sure to include both and not just one or you risk data loss. For the same reason, make sure both functions are inverses of one another. Some failsafe mechanisms are in place to prevent data loss if you misuse the serialization hooks: react-native-local-mongodb checks that never one is declared without the other, and checks that they are reverse of one another by testing on random strings of various lengths. In addition, if too much data is detected as corrupt, react-native-local-mongodb will refuse to start as it could mean you're not using the deserialization hook corresponding to the serialization hook used before (see below).
corruptAlertThreshold
(optional): between 0 and 1, defaults to 10%. react-native-local-mongodb will refuse to start if more than this percentage of the datafile is corrupt. 0 means you don't tolerate any corruption, 1 means you don't care.
compareStrings
(optional): function compareStrings(a, b) compares
strings a and b and return -1, 0 or 1. If specified, it overrides
default string comparison which is not well adapted to non-US characters
in particular accented letters. Native localCompare
will most of the
time be the right choice
If you use a persistent datastore without the autoload
option, you need to call loadDatabase
manually.
This function fetches the data from datafile and prepares the database. Don't forget it! If you use a
persistent datastore, no command (insert, find, update, remove) will be executed before loadDatabase
is called, so make sure to call it yourself or use the autoload
option.
Also, if loadDatabase
fails, all commands registered to the executor afterwards will not be executed. They will be registered and executed, in sequence, only after a successful loadDatabase
.
var Datastore = require('react-native-local-mongodb')
, db = new Datastore();
var Datastore = require('react-native-local-mongodb')
, db = new Datastore({ filename: 'asyncStorageKey' });
db.loadDatabase(function (err) {
});
var Datastore = require('react-native-local-mongodb')
, db = new Datastore({ filename: 'asyncStorageKey', autoload: true });
Persistence
Under the hood, react-native-local-mongodb's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the string in AsyncStorage, for performance reasons. The database is automatically compacted (i.e. put back in the one-line-per-document format) every time you load each database within your application.
You can manually call the compaction function with yourDatabase.persistence.compactDatafile
which takes no argument. It queues a compaction of the datafile in the executor, to be executed sequentially after all pending operations. The datastore will fire a compaction.done
event once compaction is finished.
You can also set automatic compaction at regular intervals with yourDatabase.persistence.setAutocompactionInterval(interval)
, interval
in milliseconds (a minimum of 5s is enforced), and stop automatic compaction with yourDatabase.persistence.stopAutocompaction()
.
Keep in mind that compaction takes a bit of time (not too much: 130ms for 50k records on a typical development machine) and no other operation can happen when it does, so most projects actually don't need to use it.
Compaction will also immediately remove any documents whose data line has become corrupted, assuming that the total percentage of all corrupted documents in that database still falls below the specified corruptAlertThreshold
option's value.
Durability works similarly to major databases: compaction forces the OS to physically flush data to disk, while appends to the data file do not (the OS is responsible for flushing the data). That guarantees that a server crash can never cause complete data loss, while preserving performance. The worst that can happen is a crash between two syncs, causing a loss of all data between the two syncs. Usually syncs are 30 seconds appart so that's at most 30 seconds of data. This post by Antirez on Redis persistence explains this in more details, react-native-local-mongodb being very close to Redis AOF persistence with appendfsync
option set to no
.
Inserting documents
The native types are String
, Number
, Boolean
, Date
and null
. You can also use
arrays and subdocuments (objects). If a field is undefined
, it will not be saved (this is different from
MongoDB which transforms undefined
in null
, something I find counter-intuitive).
If the document does not contain an _id
field, react-native-local-mongodb will automatically generate one for you (a 16-characters alphanumerical string). The _id
of a document, once set, cannot be modified.
Field names cannot begin by '$' or contain a '.'.
var doc = { hello: 'world'
, n: 5
, today: new Date()
, react-native-local-mongodbIsAwesome: true
, notthere: null
, notToBeSaved: undefined
, fruits: [ 'apple', 'orange', 'pear' ]
, infos: { name: 'react-native-local-mongodb' }
};
db.insert(doc, function (err, newDoc) {
});
You can also bulk-insert an array of documents. This operation is atomic, meaning that if one insert fails due to a unique constraint being violated, all changes are rolled back.
db.insert([{ a: 5 }, { a: 42 }], function (err, newDocs) {
});
db.insert([{ a: 5 }, { a: 42 }, { a: 5 }], function (err) {
});
Finding documents
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
, $not
and $where
. See below for the syntax.
You can use regular expressions in two ways: in basic querying in place of a string, or with the $regex
operator.
You can sort and paginate results using the cursor API (see below).
You can use standard projections to restrict the fields to appear in the results (see below).
Basic querying
Basic querying means are looking for documents whose fields match the ones you specify. You can use regular expression to match strings.
You can use the dot notation to navigate inside nested documents, arrays, arrays of subdocuments and to match a specific element of an array.
db.find({ system: 'solar' }, function (err, docs) {
});
db.find({ planet: /ar/ }, function (err, docs) {
});
db.find({ system: 'solar', inhabited: true }, function (err, docs) {
});
db.find({ "humans.genders": 2 }, function (err, docs) {
});
db.find({ "completeData.planets.name": "Mars" }, function (err, docs) {
});
db.find({ "completeData.planets.name": "Jupiter" }, function (err, docs) {
});
db.find({ "completeData.planets.0.name": "Earth" }, function (err, docs) {
});
db.find({ humans: { genders: 2 } }, function (err, docs) {
});
db.find({}, function (err, docs) {
});
db.findOne({ _id: 'id1' }, function (err, doc) {
});
Operators ($lt, $lte, $gt, $gte, $in, $nin, $ne, $exists, $regex)
The syntax is { field: { $op: value } }
where $op
is any comparison operator:
$lt
, $lte
: less than, less than or equal
$gt
, $gte
: greater than, greater than or equal
$in
: member of. value
must be an array of values
$ne
, $nin
: not equal, not a member of
$exists
: checks whether the document posses the property field
. value
should be true or false
$regex
: checks whether a string is matched by the regular expression. Contrary to MongoDB, the use of $options
with $regex
is not supported, because it doesn't give you more power than regex flags. Basic queries are more readable so only use the $regex
operator when you need to use another operator with it (see example below)
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
});
db.find({ planet: { $gt: 'Mercury' }}, function (err, docs) {
})
db.find({ planet: { $in: ['Earth', 'Jupiter'] }}, function (err, docs) {
});
db.find({ satellites: { $exists: true } }, function (err, docs) {
});
db.find({ planet: { $regex: /ar/, $nin: ['Jupiter', 'Earth'] } }, function (err, docs) {
});
Array fields
When a field in a document is an array, react-native-local-mongodb first tries to see if the query value is an array to perform an exact match, then whether there is an array-specific comparison function (for now there is only $size
and $elemMatch
) being used. If not, the query is treated as a query on every element and there is a match if at least one element matches.
$size
: match on the size of the array
$elemMatch
: matches if at least one array element matches the query entirely
db.find({ satellites: ['Phobos', 'Deimos'] }, function (err, docs) {
})
db.find({ satellites: ['Deimos', 'Phobos'] }, function (err, docs) {
})
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 3 } } } }, function (err, docs) {
});
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 5 } } } }, function (err, docs) {
});
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: { $gt: 2 } } } } }, function (err, docs) {
});
db.find({ satellites: { $size: 2 } }, function (err, docs) {
});
db.find({ satellites: { $size: 1 } }, function (err, docs) {
});
db.find({ satellites: 'Phobos' }, function (err, docs) {
});
db.find({ satellites: { $lt: 'Amos' } }, function (err, docs) {
});
db.find({ satellites: { $in: ['Moon', 'Deimos'] } }, function (err, docs) {
});
Logical operators $or, $and, $not, $where
You can combine queries using logical operators:
- For
$or
and $and
, the syntax is { $op: [query1, query2, ...] }
.
- For
$not
, the syntax is { $not: query }
- For
$where
, the syntax is { $where: function () { /* object is "this", return a boolean */ } }
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }] }, function (err, docs) {
});
db.find({ $not: { planet: 'Earth' } }, function (err, docs) {
});
db.find({ $where: function () { return Object.keys(this) > 6; } }, function (err, docs) {
});
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }], inhabited: true }, function (err, docs) {
});
Sorting and paginating
If you don't specify a callback to find
, findOne
or count
, a Cursor
object is returned. You can modify the cursor with sort
, skip
and limit
and then execute it with exec(callback) - a promise is returned
.
db.find({}).sort({ planet: 1 }).skip(1).limit(2).exec(function (err, docs) {
});
db.find({ system: 'solar' }).sort({ planet: -1 }).exec(function (err, docs) {
});
db.find({}).sort({ firstField: 1, secondField: -1 }) ...
Projections
You can give find
and findOne
an optional second argument, projections
. The syntax is the same as MongoDB: { a: 1, b: 1 }
to return only the a
and b
fields, { a: 0, b: 0 }
to omit these two fields. You cannot use both modes at the time, except for _id
which is by default always returned and which you can choose to omit. You can project on nested documents.
db.find({ planet: 'Mars' }, { planet: 1, system: 1 }, function (err, docs) {
});
db.find({ planet: 'Mars' }, { planet: 1, system: 1, _id: 0 }, function (err, docs) {
});
db.find({ planet: 'Mars' }, { planet: 0, system: 0, _id: 0 }, function (err, docs) {
});
db.find({ planet: 'Mars' }, { planet: 0, system: 1 }, function (err, docs) {
});
db.find({ planet: 'Mars' }).projection({ planet: 1, system: 1 }).exec(function (err, docs) {
});
db.findOne({ planet: 'Earth' }).projection({ planet: 1, 'humans.genders': 1 }).exec(function (err, doc) {
});
Counting documents
You can use count
to count documents. It has the same syntax as find
. For example:
db.count({ system: 'solar' }, function (err, count) {
});
db.count({}, function (err, count) {
});
Updating documents
db.update(query, update, options, callback)
will update all documents matching query
according to the update
rules:
query
is the same kind of finding query you use with find
and findOne
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!)
- A new document will replace the matched docs
- The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. Available field modifiers are
$set
to change a field's value, $unset
to delete a field, $inc
to increment a field's value and $min
/$max
to change field's value, only if provided value is less/greater than current value. To work on arrays, you have $push
, $pop
, $addToSet
, $pull
, and the special $each
and $slice
. See examples below for the syntax.
options
is an object with two possible parameters
multi
(defaults to false
) which allows the modification of several documents if set to true
upsert
(defaults to false
) if you want to insert a new document corresponding to the update
rules if your query
doesn't match anything. If your update
is a simple object with no modifiers, it is the inserted document. In the other case, the query
is stripped from all operator recursively, and the update
is applied to it.
returnUpdatedDocs
(defaults to false
, not MongoDB-compatible) if set to true and update is not an upsert, will return the array of documents matched by the find query and updated. Updated documents will be returned even if the update did not actually modify them
callback
(optional) signature: (err, numAffected, affectedDocuments, upsert)
. Warning: the API was changed between v1.7.4 and v1.8. Please refer to the change log to see the change.
- For an upsert,
affectedDocuments
contains the inserted document and the upsert
flag is set to true
.
- For a standard update with
returnUpdatedDocs
flag set to false
, affectedDocuments
is not set.
- For a standard update with
returnUpdatedDocs
flag set to true
and multi
to false
, affectedDocuments
is the updated document.
- For a standard update with
returnUpdatedDocs
flag set to true
and multi
to true
, affectedDocuments
is the array of updated documents.
Note: you can't change a document's _id.
db.update({ planet: 'Jupiter' }, { planet: 'Pluton'}, {}, function (err, numReplaced) {
});
db.update({ system: 'solar' }, { $set: { system: 'solar system' } }, { multi: true }, function (err, numReplaced) {
});
db.update({ planet: 'Mars' }, { $set: { "data.satellites": 2, "data.red": true } }, {}, function () {
db.update({ planet: 'Mars' }, { $set: { data: { satellites: 3 } } }, {}, function () {
});
});
db.update({ planet: 'Mars' }, { $unset: { planet: true } }, {}, function () {
});
db.update({ planet: 'Pluton' }, { planet: 'Pluton', inhabited: false }, { upsert: true }, function (err, numReplaced, upsert) {
});
db.update({ planet: 'Pluton' }, { $inc: { distance: 38 } }, { upsert: true }, function () {
});
db.update({ _id: 'id6' }, { $push: { fruits: 'banana' } }, {}, function () {
});
db.update({ _id: 'id6' }, { $pop: { fruits: 1 } }, {}, function () {
});
db.update({ _id: 'id6' }, { $addToSet: { fruits: 'apple' } }, {}, function () {
});
db.update({ _id: 'id6' }, { $pull: { fruits: 'apple' } }, {}, function () {
});
db.update({ _id: 'id6' }, { $pull: { fruits: $in: ['apple', 'pear'] } }, {}, function () {
});
db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana', 'orange'] } } }, {}, function () {
});
db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana'], $slice: 2 } } }, {}, function () {
});
db.update({ _id: 'id1' }, { $min: { value: 2 } }, {}, function () {
});
db.update({ _id: 'id1' }, { $min: { value: 8 } }, {}, function () {
});
Removing documents
db.remove(query, options, callback)
will remove all documents matching query
according to options
query
is the same as the ones used for finding and updating
options
only one option for now: multi
which allows the removal of multiple documents if set to true. Default is false
callback
is optional, signature: err, numRemoved
db.remove({ _id: 'id2' }, {}, function (err, numRemoved) {
});
db.remove({ system: 'solar' }, { multi: true }, function (err, numRemoved) {
});
db.remove({}, { multi: true }, function (err, numRemoved) {
});
Indexing
react-native-local-mongodb 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
. The indexed values cannot be of type array of object.
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.
- expireAfterSeconds (number of seconds, optional): if set, the created index is a TTL (time to live) index, that will automatically remove documents when the system date becomes larger than the date on the indexed field plus
expireAfterSeconds
. Documents where the indexed field is not specified or not a Date
object are ignored
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.
db.ensureIndex({ fieldName: 'somefield' }, function (err) {
});
db.ensureIndex({ fieldName: 'somefield', unique: true }, function (err) {
});
db.ensureIndex({ fieldName: 'somefield', unique: true, sparse: true }, function (err) {
});
db.insert({ somefield: 'react-native-local-mongodb' }, function (err) {
db.insert({ somefield: 'react-native-local-mongodb' }, function (err) {
});
});
db.removeIndex('somefield', function (err) {
});
db.ensureIndex({ fieldName: 'createdAt', expireAfterSeconds: 3600 }, function (err) {
});
db.ensureIndex({ fieldName: 'expirationDate', expireAfterSeconds: 0 }, 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).
License
See License