JavaScript implementation of the memory database Lokijs Introduction and introductory examples _javascript tips

Source: Internet
Author: User
Tags mongodb

Lokijs is a memory database that puts performance considerations in the first place.
Lokijs supports indexing and faster document access, performing very well (nearly 500,000 ops/seconds). Its built-in Dynamicview classes can be used for indexing of subsets of data, even faster performance.

* Read this article to see the performance of Lokijs.

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

Lokijs can be run on the Node.js side and the browser side.

JavaScript is a simple, universal language, so it's very easy and efficient to develop JavaScript databases. If your MongoDB is not retired, you may find Lokijs a better solution in the following situations:

1. Mobile applications-especially HTML applications. (Cordova, Phonegap)
2.node.js built-in data storage for small to medium application design
3. Applications built into the desktop (Node Webkit)

Lokijs is supporting stand-alone servers and can be accessed using HTTP/TCP clients.

Choose the paradigm you like best

The Lokijs function fully utilizes the power of JavaScript.
If functional programming is your preferred style, you will certainly prefer to use views to query the data.
You can also use your preferred MongoDB Shell to query text objects.

Quick Start

Installation

Lokijs can be installed in NPM and Bower. Run:

Copy Code code as follows:

NPM Install Lokijs

Or
Copy Code code as follows:

Bower Install Lokijs

Use

To create a database:

Copy Code code as follows:

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

Pass in the JSON file where you need to save the data

To create a dataset:

Copy Code code as follows:

var children = db.addcollection (' Children ')

To insert a document:

Copy Code code as follows:

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

To get a document:

Copy Code code as follows:

Children.get (1); Returns Sleipnir
Children.find ({' name ': ' Sleipnir '})
Children.find ({legs: {' $gt ': 2}})

To create a dynamic view:

Copy Code code as follows:

var legs = children.adddynamicview (' legs ');
Legs.applyfind ({legs: {' $gt ': 2})
Legs.applysimplesort (' legs ');
Legs.data ();

MapReduce (Data aggregation):

Copy Code code 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);
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.