JavaScript implementation of the in memory database LokiJS introduction and introductory examples
- 2020-03-30 04:18:23
- OfStack
* read (link: http://, https://github.com/techfort/LokiJS/wiki/Indexing-and-Query-Performance) have a look at the LokiJS performance.
LokiJS runs on the node. js side and the browser side.
JavaScript is a simple, general-purpose language to learn, so development in JavaScript databases is very easy and efficient. If your MongoDB is not yet retired, you may find LokiJS a better solution in the following situations:
1. Mobile apps - especially HTML apps. (Cordova, Phonegap)
2.Node.js built-in data storage designed for small to medium-sized applications
3. Applications built into the desktop (Node Webkit)
Choose your favorite paradigm
LokiJS takes full advantage of the power of JavaScript.
If functional programming is your preferred style, you'll definitely enjoy using views to query data.
You can also query text objects using your preferred MongoDB shell.
Quick start
The installation
LokiJS can be installed in NPM and bower.
npm install lokijs
or
bower install lokijs
use
Create database:
var db = new loki('loki.json')
Pass in the JSON file where you want to save the data
Create data set:
var children = db.addCollection('children')
Insert document:
children.insert({name:'Sleipnir', legs: 8})
children.insert({name:'Jormungandr', legs: 0})
children.insert({name:'Hel', legs: 2})
Obtain documents:
children.get(1); // returns Sleipnir
children.find( {'name':'Sleipnir'} )
children.find( { legs: { '$gt' : 2 } } )
Create dynamic views:
var legs = children.addDynamicView('legs');
legs.applyFind( { legs: { '$gt' : 2 } )
legs.applySimpleSort('legs');
legs.data();
MapReduce (data aggregation) :
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);
});