Skip to content
Eric edited this page Apr 4, 2024 · 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).

MongOGX can also be used with OGX.JS in OML as a OSE script such as

 {{mongogx myDb.MyCollection.find(MyQuery)}}

Installation

Go to the release page and download the latest build or, if you are using npm

 npm install @globules-io/mongogx

This will copy mongogx.min.js in www/js/lib/globules. If index.html is found in www/, the link will be added to the index file.

Supported Standard Features

Collections

 insert
 insertOne
 insertMany
 update
 updateOne
 updateMany
 replaceOne
 deleteOne
 deleteMany
 remove
 find
 findOne 1.2.2+
 toArray
 toJSON

Note that remove empties the collection but does not delete the actual collection. To delete use deleteCollection

Filter Operators

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

Also note that since version 1.2.2, you can use implied eq operator, such as {property:value} instead of {property{eq:value}}

Update operators

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

Additional methods

 setCollection
 getCollections
 getCollection
 createCollection
 deleteCollection
 clearCollection
 hasCollection

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.SESSION_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
   }
   encryption:false|{scheme:'AES', key:'mysecretkey'} //AES Encyption
 }

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

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