Skip to content

Commit eb8ea94

Browse files
committed
Added findOne method, find now handles implied eq
1 parent f32ba2b commit eb8ea94

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

collection.mongogx.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ OGX.MongogxCollection = class{
146146
If there is shit after any, stop and return that multi nesting find is to do
147147
If not supported by mongo (it does't seem to be), then let's not do it (doable but slow, recursions of x arrays)
148148
*/
149+
150+
//if direct prop/val pair, convert to eq
151+
if(typeof(__query[prop]) !== 'object'){
152+
var f = {};
153+
f = {eq:__query[prop]};
154+
__query[prop] = f;
155+
}
156+
149157
base = data[_id];
150158
path = prop.split('.');
151159
if(path.length > 1){
@@ -221,7 +229,10 @@ OGX.MongogxCollection = class{
221229
}
222230
return docs;
223231
}
224-
232+
233+
findOne(__query){
234+
return this.find(__query, 1);
235+
}
225236

226237
//Return as array
227238
toJSON(){

core.mongogx.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,25 @@ OGX.Mongogx = class{
249249
}
250250
return false;
251251
}
252+
253+
findOne(__query){
254+
if(this._isSet()){
255+
let doc = this.data.db[this.database].getCollection().findOne(__query);
256+
if(this.options.format){
257+
doc = this._objToArr(doc)[0];
258+
return doc;
259+
}
260+
}
261+
return false;
262+
}
252263

253264
/*INTERNAL STUFF*/
254265
_objToArr(__obj){
255266
let arr = [];
256267
for(let a in __obj){
257268
if(__obj.hasOwnProperty(a)){
258269
arr.push(__obj[a]);
259-
}
270+
}
260271
}
261272
return arr;
262273
}

0 commit comments

Comments
 (0)