Translated from: http://www.html5cn.org/article-7091-1.html
Lokijs a lightweight document-oriented database, implemented by JavaScript, performs better than anything else. The goal is to use JavaScript objects as documents, then store them, and retrieve the data in the same way. Lokijs can run on node. JS and all JavaScript-enabled browsers, mobile applications.
Lokijs supports field indexing for faster file access and is also fairly well-performing (nearly 500,000 ops/s). Its built-in Dynamicview class can also be used for indexing subsets of data to achieve faster performance. Lokijs supports collections, much like MongoDB, and saves data in JSON format to the hard disk (depending on the session to restore state), so your data is portable.
The
ideal scenario for using Lokijs:
- Mobile apps--especially based on HTML5 (e.g. CORDOVA,PHONEGAP, etc.)
- Data storage for small-to medium-sized applications based on node. js
- Desktop applications with Node-webkit
- Lokijs is still in the alpha phase. The source is hosted on GitHub.
Quick installation:
[JS]
- NPM Install Lokijs
- Or
- Bower Install Lokijs
Copy Code
Create a database:
- var db = new Loki (' Loki.json ')
- Pass the filename where to persist data
Copy Code
Create a collection:
- var children = db.addcollection (' Children ')
Copy Code
Insert a document:
- Children.insert ({name: ' Sleipnir ', legs:8})
- Children.insert ({name: ' Jormungandr ', legs:0})
- Children.insert ({name: ' Hel ', legs:2})
Copy Code
Retrieving/Obtaining Documents:
- Children.get (1); Returns Sleipnir
- Children.find ({' name ': ' Sleipnir '})
- Children.find ({legs: {' $gt ': 2}})
Copy Code
Create a dynamic view:
- var legs = children.adddynamicview (' legs ');
- Legs.applyfind ({legs: {' $gt ': 2})
- Legs.applysimplesort (' legs ');
- Legs.data ();
Copy Code
Mapreduce:
- 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);
- });
Copy Code
Lokijs Related information:
Official website: http://lokijs.org/
managed Address: Https://github.com/techfort/LokiJS
Source Address: http://www.csdn.net/article/2014-11-05/2822493-LokiJS?reload=1
"Go" Lokijs: A lightweight database implemented with pure JavaScript