mongo using mongoose in node want to use or and in query

I have a situation where I have to check name of the company and code of the company and if both match with existing than it should say exists, or if one of them is matched in existing db then also it should be saying it exists. How would I use in mongo? So if the data receive isDeleted true than I also want to add in if section that checks if not adding than goes for checking id and update, than I also want to pass isDelete, so that if any data received which is previously deleted so that it can set to false again. how would I handle this delete scenario?

{
    "userId":"tt838984984s",
  "company":{
  "addressee" : {
      "addressee" : "Ms.1", 
      "title_name" : "", 
      "salutation" : "Drks:", 
      "comments" : "", 
      "email" : "[email protected]", 
      "phone" : "123456666", 
      "fax" : "", 
      "extention_group" : "", 
      "ext" : ""
  }, 
  "abbreviation" : "", 
  "care_of" : "", 
  "address_1" : "HELLO2",
  "address_2" : "", 
  "address_3" : "", 
  "state" : "CA", 
  "zip" : "90024", 
  "is_deleted" : true, 
  "company_code" : "ABACAB", 
  "parent_company" : null, 
  "name" : "Abacab", 
  "createdBy" : "Data lolz", 
  "modifiedBy" : "Data", 
  "createdDate" : "2019-08-22T19:10:50.000+0000", 
  "modifiedDate" : "2019-08-22T19:10:50.000+0000", 
  "company_id_legacy" :1246, 
  "__v" : 0, 
  "is_registered_for" : false,

},
}


  is_deleted == false
    if(!isAdd) {
          filter["_id"] ={ "$ne" : id};
           }
        let filter = {
                 name: { $regex: new RegExp(`^${company.name}$`, 'i') },
                 company_code: { $regex: new RegExp(`^${company.company_code}$`, 'i') }
    }
    cModel.find(filter, function (err, docs) {
                            if (docs.length) {
                                result.error = "Name already exists: " + company.name;
                                console.log("Name already exists", null);
                                let resp = api_respose.getSuccessResponse(process.env.WEB_URI, result.error);
                                resolve(resp);
                            }
    else{
     ///saving here
    }

Now suppose I pass that JSON, and if there's is_deleted = false (this is Json while adding new entry in db) And now if there's some old entry with name or company_code exists in db with is_delete =true then it throws error that name already exits

Now my question is how to resolve this scenario? Like I want to overwrite that file with new entry or is there any other way of doing this?

Answers:

Answer

You can use $or operator

  let filter = {
     "$or":[
             name: { $regex: new RegExp(`^${company.name}$`, 'i') },
             company_code: { $regex: new RegExp(`^${company.company_code}$`, 'i') 
            ]
}
}
cModel.find(filter, function (err, docs) {
                        if (docs.length) {
                            result.error = "Name already exists: " + company.name;
                            console.log("Name already exists", null);
                            let resp = api_respose.getSuccessResponse(process.env.WEB_URI, result.error);
                            resolve(resp);
                        }
else{
 ///saving here
}

refer https://docs.mongodb.com/manual/reference/operator/query/or/ for more info

Answer

If I have correctly understood, you need using $or inside of $and function in Mongodb or mongoose, both of them supports these two functions. For example (in mongoose):

cModel.find(
        {
            $and: [
                {
                    $or: [
                        { name: { $regex: new RegExp(`^${company.name}$`, 'i') } },
                        { company_code: { $regex: new RegExp(`^${company.company_code}$`, 'i') } }
                    ]
                },
                {
                    is_delete: true
                }
            ]
        }, function (err, docs) { 
            if(docs){
                // throw error!
            } if (!docs) {
                // enter your new doc here with cModel.create(yourDoc, function(error, data){});
            }
        }
    );

complete documentation for this function is here:

https://docs.mongodb.com/manual/reference/operator/query/and/

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.