I have problems with findOneAndUpdate, does not return an error when one of the search data is undefined

When I do a search with "findOneAndUpdate" and one of my search parameters is "undefined" I do not get an error but is the object searched. this is the code:

var q = Q.defer();

    var findOneQuery = {
        _id: restId, 
        versionId: document.version // if this parameter is undefined
    };

    this.findOneAndUpdate(findOneQuery, {$set: document, $inc: {versionId: 1}}, {upsert: true, new: true}, function (updateError, updateDocument) {
        if (updateError) {
            q.reject(updateError);
        }
        else {
          q.resolve(updateDocument);
        }
    });
    return q.promise;

I think it should return an error if I'm wrong What should I do to search for the two parameters sent and not just by one of them?


ANSWERS:


You can easily write a wrapper method around findOneandUpdate that would precisely do what you want with your reqs.

function myFindOneAndUpdate(monObj,query,update,options,callback){
  //validateINput check if the params in query object are undefined
   if(validateInput(query)){
     monObj.findOneAndUpdate(query,update,options,callback)
}else{
 throw new Error('InvalidInput');
}

}


If parameter is undefined, then mongodb try to find records in which parameter is undefined. So it would not throw any error.

if you want that versionId should be never null/undefined. then you can validate inputs before passing to db query.

You can use this module:

https://www.npmjs.com/package/validator


 MORE:


 ? How to return a variable from a Q Node promise?
 ? Loop with EACH in Q KDB+
 ? Loop with EACH in Q KDB+
 ? Loop with EACH in Q KDB+
 ? Inconsistent behaviour with each and peach in KDB
 ? Using flip inside each not working in KDB
 ? How to highlight a filed cell when user clicks on a button
 ? How to highlight a filed cell when user clicks on a button
 ? How to highlight a filed cell when user clicks on a button
 ? How to highlight a table cell (TD) when radio button is clicked with JavaScript/JQuery?