-
Notifications
You must be signed in to change notification settings - Fork 2
Home
MongOGX
is a simple JavaScript based document store/database inspired by MongoDB. It supportslocal storage
andapplication 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 inOML
as aOSE
script such as
{{mongogx myDb.MyCollection.find(MyQuery)}}
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
inwww/js/lib/globules
. Ifindex.html
is found inwww/
, the link will be added to the index file.
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 usedeleteCollection
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}}
currentDate
inc
min
max
mul
rename
set
setOnInsert [no support]
unset
setCollection
getCollections
getCollection
createCollection
deleteCollection
clearCollection
hasCollection
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 collectionSELECTED_COLLECTION
.
{
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
)
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'}}
//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'});
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);
elemMatch
Pipeline/Aggregation