Skip to content
Eric edited this page May 23, 2019 · 63 revisions

Welcome to the MongOGX wiki!

MongOGX is a simple JavaScript based document store/database inspired by MongoDB. It supports local storage and application data storage (via Cordova - cordova-plugin-file) storage modes. It also supports encryption (via code.google.com/p/crypto-js - added to this repo).

Supported Features

Collections

 insert
 insertOne
 insertMany
 update
 updateOne
 updateMany
 replaceOne
 deleteOne
 deleteMany
 find
 toArray
 toJSON

Filter Operators

 eq
 neq
 gt
 gte
 lt
 lte
 in     
 nin
 text
 regex
 mod
 all
 size
 elemMatch [no support]

Update operators

 currentDate
 inc
 min
 max
 mul
 rename
 set
 setOnInsert [no support]
 unset

Instantiate

let mongogx = new OGX.Mongogx(_SELECTED_DATABASE_, _SELECTED_COLLECTION_, _OPTIONS_);	
//_SELECTED_DATABASE_, optional, the database to select upon start, defaults to null
//_SELECTED_COLLECTION_, optional, the collection to select upon start, defaults to null
//_OPTIONS_, optional.

MongOGX will create the database SELECTED_DATABASE if passed and does not exist, as well as the collection SELECTED_COLLECTION.

Options

{
   storage: OGX.Mongogx.LOCAL_STORAGE | OGX.Mongogx.APP_STORAGE, //defaults to OGX.Mongogx.LOCAL_STORAGE
   format: OGX.Mongogx.FORMAT_ARRAY | OGX.Mongogx.FORMAT_OBJECT, //defaults to OGX.Mongogx.FORMAT_OBJECT
   write_concern:{
         mode: OGX.Mongogx.WRITE_DIRECT, //Only supported mode for now, default
         delay:5 //timeout before operation
   },
   load_concern:{
        mode: OGX.Mongogx.LOAD_FULL |  OGX.Mongogx.LOAD_STICK | OGX.Mongogx.LOAD_DROP
   },
   encryption:false|{scheme:'AES', key:'mysecretkey'} //ASE Encyption
 }

MongOGX by default output the result of a search (find) as a collection (FORMAT_OBJECT) or as an array (FORMAT_ARRAY)

Load modes (wip - v0.2 - breaking changes in stored data)

In order to save memory usage, multiple load_concern modes are available: LOAD_FULL (default), LOAD_DROP and LOAD_STICK.

LOAD_DROP

To limit memory usage and allow larger datasets to be handled, you can set the load_concern to LOAD_DROP. This will only load a collection on demand and drop it from memory is the collection has not been used for a certain time. This mode is only available for APP_STORAGE storage mode. Due to the storage limit of localStorage (between 2 and 10 MB depending on the environment), it wouldn't make sense to load and drop from localStorage

 {..., load_concern:{mode:OGX.Mongogx.LOAD_DROP, delay:180000}}

LOAD_STICK

You can also reduce memory by using the LOAD_STICK which loads db/collections on demand and then keep them loaded into memory. This mode is both available with localStorage and appStorage

 {..., load_concern:{mode:OGX.Mongogx.LOAD_STICK}}

LOAD_FULL

Finally, the default load_concern mode is set to LOAD_FULL, which loads the entire db/collections into memory.

 {..., load_concern:{mode:OGX.Mongogx.LOAD_FULL}}

Encryption

MongOGX supports AES encryption. To write/read encrypted data, you have to declare the scheme (AES only for now) and the key, such as

 {..., encryption:{scheme:'AES', key:'mysecretkey'}}

Getting started

 //Create instance
 let mongogx = new OGX.Mongogx();	

 //Create a db
 mongogx.createDatabase('my_project');	

 //Set as active/current db
 mongogx.setDatabase('my_project');	

 //Create a collection
 mongogx.createCollection('users');		

 //Set as active/current collection
 mongogx.setCollection('users');	

 //Insert a bunch of non normalized documents
 mongogx.insert({first_name:'Eric', age:42, sex:'male', location:{state:'ON', city:'Toronto'}, favorites:{meals:[{name:'pizza'}, {name:'pasta'}]}});
 mongogx.insert({first_name:'Tania', age:38, sex:'female', location:{state:'ON', city:'Toronto'}}); 
 mongogx.insert({first_name:'Julien', age:45, sex:'male', location:{state:null, city:'Stockholm'}});
 mongogx.insert({first_name:'George', age:55, sex:'male', location:{state:'QC', city:'Montreal'}, arts:{martial:['kickboxing', 'wresting']}});	

 let collection;

 //Find all documents given a state
 collection = mongogx.find({'location.state':'ON'});

 //Find all users older than 20
 collection = mongogx.find({age:{$gt:20}});

 //Find all males older than 20 living in Toronto
 collection = mongogx.find({age:{$gt:20}, sex:'male', 'location.city':'Toronto'});

 //Find all users with favorite meal matching pizza
 collection = mongogx.find({'favorites.meals.name':'pizza'});

 //Specifically target an array to match a property
 collection = mongogx.find({'favorites.meals.1.name':'pasta'});

Cordova

If you are using APP_STORAGE via Cordova, you must pass and wait for a callback fired when the database is ready, such as

 function onDbReady(){
       //you can now use mongogx
 }

 let options = {
      storage:OGX.Mongogx.APP_STORAGE,
      write_concern:{
          mode:OGX.Mongogx.WRITE_DIRECT, //Only supported mode for now, default
          delay:5 //timeout before operation
      },
      callback:onDbReady
 };   

 let mongogx = new OGX.Mongogx(null, null, options);  

To do

 elemMatch
 Pipeline/Aggregation
 Seperate db/collections files in APP_STORAGE mode + load_concern (wip)
Clone this wiki locally