Home >Web Front-end >JS Tutorial >Introduction to LokiJS, an in-memory database implemented by JavaScript, and introductory examples_javascript skills

Introduction to LokiJS, an in-memory database implemented by JavaScript, and introductory examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:31:062077browse

LokiJS is an in-memory database that puts performance considerations first.
LokiJS supports indexing and faster document access, and the execution performance is very good (nearly 500,000 OPS/second). Its built-in DynamicView class can be used to index subsets of data for even faster performance.

*Read this article to take a look at the performance of LokiJS.

LokiJS supports collections (datasets), much like MongoDB, and saves data to disk in JSON format, so your data is portable.

LokiJS can run on Node.js side and browser side.

JavaScript is an easy-to-learn, universal language, so database development in JavaScript is very easy and very efficient. If your MongoDB has not yet been retired, you may find LokiJS to be a more ideal 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. Applications built into the desktop (Node Webkit)

LokiJS is supporting standalone servers, accessible using http/tcp clients.

Choose your favorite paradigm

The functionality of LokiJS fully harnesses the power of JavaScript.
If functional programming is your preferred style, then you'll definitely enjoy using 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:

Copy code The code is as follows:

npm install lokijs

or
Copy code The code is as follows:

bower install lokijs

Use

Create database:

Copy code The code is as follows:

var db = new loki('loki.json')

Input the JSON file you need to save the data

Create dataset:

Copy code The code is as follows:

var children = db.addCollection('children')

Insert document:

Copy code The code is as follows:

children.insert({name:'Sleipnir', legs: 8})
children.insert({name:'Jormungandr', legs: 0})
children.insert({name:'Hel', legs: 2})

Get documents:

Copy code The code is as follows:

children.get(1); // returns Sleipnir
children.find( {'name':'Sleipnir'} )
children.find( { legs: { '$gt' : 2 } } )

Create dynamic view:

Copy code The code is as follows:

var legs = children.addDynamicView('legs');
legs.applyFind( { legs: { '$gt' : 2 } )
legs.applySimpleSort('legs');
legs.data();

MapReduce (data aggregation):

Copy code 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);
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn