问题描述
我遇到的情况是,我必须检查公司名称和公司代码,并且如果两者都与现有的匹配,则应该说存在,或者如果其中之一与现有数据库匹配,那么也应该说出来存在.我将如何在mongo中使用?因此,如果数据接收的isDeleted为true,那么我也想添加if部分来检查是否未添加,而不是去检查id和更新,那么我也想传递isDelete,以便接收到的任何先前删除过的数据都这样.可以再次设置为false.我将如何处理这种删除方案?
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" : "opus7ent@example.com",
"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
}
现在假设我传递了该JSON,并且如果存在is_deleted = false
(这是在db中添加新条目时的Json)现在,如果在数据库中存在带有is_delete =true
的name
或company_code
的旧条目,则会引发错误,提示 name已经退出
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?
推荐答案
如果我正确理解,则需要在Mongodb或mongoose的$and
函数内部使用$or
,它们都支持这两个函数.例如(猫鼬):
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/运算符/查询/和/
这篇关于mongo在节点要使用or和in的查询中使用mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!