LokiJS is an in-memory database that puts performance considerations first.
LokiJS supports indexing and faster document access, with very good execution performance (nearly 500,000 OPS/sec). Its built-in DynamicView class can be used to index data subsets and even get faster performance.
*Read this article to see the performance of LokiJS.
LokiJS supports collections (datasets), which is very similar to MongoDB, and saves data to disk in JSON format, so your data is portable.
LokiJS can run on Node.js and browser side.
JavaScript is a simple and easy to learn and universal language, so it is very easy to develop a JavaScript database and is very efficient. If your MongoDB is not retired, you may find LokiJS to be a more ideal solution in these situations:
1. Mobile applications - especially HTML applications. (Cordova, Phonegap)
2.Node.js built-in data storage designed for small to medium-sized applications
3. Built-in applications in desktop (Node Webkit)
LokiJS is supporting standalone servers and can be accessed using http/tcp client.
Choose your favorite paradigm
LokiJS's functionality fully utilizes the power of JavaScript.
If functional programming is your preferred style, you will definitely like to use views to query data.
You can also use your preferred MongoDB shell to query text objects.
Quick Start
Install
LokiJS can be installed in npm and bower. Run:
The code copy is as follows:
npm install lokijs
or
The code copy is as follows:
bower install lokijs
use
Create a database:
The code copy is as follows:
var db = new loki('loki.json')
Pass in the JSON file you need to save the data
Create a dataset:
The code copy is as follows:
var children = db.addCollection('children')
Insert the document:
The code copy is as follows:
children.insert({name:'Sleipnir', legs: 8})
children.insert({name:'Jormungandr', legs: 0})
children.insert({name:'Hel', legs: 2})
Get the documentation:
The code copy is as follows:
children.get(1); // returns Sleipnir
children.find( {'name':'Sleipnir'} )
children.find( { legs: { '$gt' : 2 } } )
Create a dynamic view:
The code copy is as follows:
var legs = children.addDynamicView('legs');
legs.applyFind( { legs: { '$gt' : 2 } )
legs.applySimpleSort('legs');
legs.data();
MapReduce (data aggregation):
The code copy is as follows:
children.mapReduce(
function( obj ){ return obj.legs; } ,
function( array ) {
var sum = 0;
for (var i=0; i < array.length; i++ ){
sum += array[i];
}
return ( sum / array.length ).toFixed(2);
});