Skip to content

Commit

Permalink
Added findOne method, find now handles implied eq
Browse files Browse the repository at this point in the history
  • Loading branch information
globules-io committed Apr 4, 2024
1 parent f32ba2b commit eb8ea94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion collection.mongogx.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ OGX.MongogxCollection = class{
If there is shit after any, stop and return that multi nesting find is to do
If not supported by mongo (it does't seem to be), then let's not do it (doable but slow, recursions of x arrays)
*/

//if direct prop/val pair, convert to eq
if(typeof(__query[prop]) !== 'object'){
var f = {};
f = {eq:__query[prop]};
__query[prop] = f;
}

base = data[_id];
path = prop.split('.');
if(path.length > 1){
Expand Down Expand Up @@ -221,7 +229,10 @@ OGX.MongogxCollection = class{
}
return docs;
}


findOne(__query){
return this.find(__query, 1);
}

//Return as array
toJSON(){
Expand Down
13 changes: 12 additions & 1 deletion core.mongogx.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,25 @@ OGX.Mongogx = class{
}
return false;
}

findOne(__query){
if(this._isSet()){
let doc = this.data.db[this.database].getCollection().findOne(__query);
if(this.options.format){
doc = this._objToArr(doc)[0];
return doc;
}
}
return false;
}

/*INTERNAL STUFF*/
_objToArr(__obj){
let arr = [];
for(let a in __obj){
if(__obj.hasOwnProperty(a)){
arr.push(__obj[a]);
}
}
}
return arr;
}
Expand Down

0 comments on commit eb8ea94

Please sign in to comment.