This article mainly introduces the introduction and getting started examples of the memory database LokiJS implemented by JavaScript. LokiJS is a memory database that puts performance considerations first and uses JavaScript for writing, if you need it, refer to LokiJS as a memory database, which puts performance considerations first.
LokiJS supports indexing and faster document access, with excellent execution performance (nearly 0.5 million OPS/second ). Its built-in DynamicView class can be used to index data subsets, or even get faster performance.
* Read https://github.com/techfort/LokiJS/wiki/Indexing-and-Query-Performance "> This article takes a look at LokiJS performance.
LokiJS supports collections (Dataset), which is similar to MongoDB and saves data to disks in JSON format. Therefore, your data can be transplanted.
LokiJS can run on Node. js and browser.
JavaScript is a simple, easy-to-learn and common language, so it is very easy and efficient to develop javascript databases. If your MongoDB has not retired, you may find that LokiJS is a better solution in the following 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 on the desktop (Node Webkit)
LokiJS is supporting independent servers and can be accessed using http/tcp clients.
Select your favorite paradigm
LokiJS fully utilizes the power of JavaScript.
If function programming is your preferred style, you will certainly 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 is as follows:
Npm install lokijs
Or
The Code is as follows:
Bower install lokijs
Use
Create a database:
The Code is as follows:
Var db = new loki ('loki. json ')
Input the JSON file that you want to save the data
Create a dataset:
The Code is as follows:
Var children = db. addCollection ('children ')
Insert document:
The Code is as follows:
Children. insert ({name: 'sleipnir ', legs: 8 })
Children. insert ({name: 'jormungandand', legs: 0 })
Children. insert ({name: 'hel', legs: 2 })
Get document:
The Code is as follows:
Children. get (1); // returns Sleipnir
Children. find ({'name': 'sleipnir '})
Children. find ({legs: {'$ gt': 2 }})
Create a Dynamic View:
The Code is as follows:
Var legs = children. addDynamicView ('legs ');
Legs. applyFind ({legs: {'$ gt': 2 })
Legs. applySimpleSort ('legs ');
Legs. data ();
MapReduce (data aggregation ):
The Code 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 );
});