From eb8ea948a8d1aae980fee28d34c5b2fdcdd44593 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 4 Apr 2024 17:59:21 +0200 Subject: [PATCH] Added findOne method, find now handles implied eq --- collection.mongogx.js | 13 ++++++++++++- core.mongogx.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/collection.mongogx.js b/collection.mongogx.js index 538524b..890498f 100644 --- a/collection.mongogx.js +++ b/collection.mongogx.js @@ -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){ @@ -221,7 +229,10 @@ OGX.MongogxCollection = class{ } return docs; } - + + findOne(__query){ + return this.find(__query, 1); + } //Return as array toJSON(){ diff --git a/core.mongogx.js b/core.mongogx.js index c6e562a..3f136d7 100644 --- a/core.mongogx.js +++ b/core.mongogx.js @@ -249,6 +249,17 @@ 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){ @@ -256,7 +267,7 @@ OGX.Mongogx = class{ for(let a in __obj){ if(__obj.hasOwnProperty(a)){ arr.push(__obj[a]); - } + } } return arr; }