如何在 Mongodb 中使用与正则表达式匹配的聚合开关案例?
How to use match as with Regular Expression in Mongodb with in Aggregate switch case?
这是我所做的。
$AddFields 内部
{
ClientStatus:{
$switch: {
branches: [
{ case: {
$eq:
[
"$CaseClientStatus",
/In Progress/i
]},
then:'In Progress'
},
{ case: {
$eq:
[
"$CaseClientStatus",
{regex:/Cancelled/i}
],
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
}
但是我的 ClientStatus 仅显示 Complete、Other、Case On Hold 而不是使用正则表达式指定的那个。虽然字段包含这些词。
这是文档之一
{
"CandidateName": "Bruce Consumer",
"_id": "61b30daeaa237672bb7a17cc",
"CaseClientStatus": "Background Check Case In Progress",
"TAT": "N/A",
"CaseCloseDate": null,
"FormationAutomationStatus": "Automated",
"MethodOfDataSupply": "Automated",
"Status": "Background Case In Progress",
"CreatedDate": "2021-12-10T08:19:58.389Z",
"OrderId": "Ord3954",
"PONumber": "",
"Position": "",
"FacilityCode": "",
"IsCaseClose": false,
"Requester": "Shah Shah",
"ReportErrorList": 0
}
假设您使用的是 4.2 或更高版本(您应该是,因为 4.2 大约 2 年前就发布了),那么 $regexFind
函数可以满足您的需求。在 4.2 之前,正则表达式只能在 $match
运算符中使用,不能在复杂的聚合表达式中使用。您上面的尝试令人钦佩,但 //
正则表达式语法 而不是 做您认为应该做的事情。值得注意的是,{regex:/Cancelled/i}
只是用键 regex
和字符串值 /Cancelled/i
(包括斜线)创建一个新对象,这显然不等于 $CaseClientStatus
中的任何内容。这是一个解决方案:
ClientStatus:{
$switch: {
branches: [
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /In Progress/i}}]
}, then:'In Progress'},
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /Cancelled/i}}]
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
看起来您正在尝试采用某种形式自由的状态“描述”字段并从中创建强大的枚举状态。我建议您的 $ClientStatus
输出更像代码,例如IN_PROGRESS, COMPLETE, CXL
等等。消除大小写,当然还有空格。
这是我所做的。
$AddFields 内部
{
ClientStatus:{
$switch: {
branches: [
{ case: {
$eq:
[
"$CaseClientStatus",
/In Progress/i
]},
then:'In Progress'
},
{ case: {
$eq:
[
"$CaseClientStatus",
{regex:/Cancelled/i}
],
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
}
但是我的 ClientStatus 仅显示 Complete、Other、Case On Hold 而不是使用正则表达式指定的那个。虽然字段包含这些词。
这是文档之一
{
"CandidateName": "Bruce Consumer",
"_id": "61b30daeaa237672bb7a17cc",
"CaseClientStatus": "Background Check Case In Progress",
"TAT": "N/A",
"CaseCloseDate": null,
"FormationAutomationStatus": "Automated",
"MethodOfDataSupply": "Automated",
"Status": "Background Case In Progress",
"CreatedDate": "2021-12-10T08:19:58.389Z",
"OrderId": "Ord3954",
"PONumber": "",
"Position": "",
"FacilityCode": "",
"IsCaseClose": false,
"Requester": "Shah Shah",
"ReportErrorList": 0
}
假设您使用的是 4.2 或更高版本(您应该是,因为 4.2 大约 2 年前就发布了),那么 $regexFind
函数可以满足您的需求。在 4.2 之前,正则表达式只能在 $match
运算符中使用,不能在复杂的聚合表达式中使用。您上面的尝试令人钦佩,但 //
正则表达式语法 而不是 做您认为应该做的事情。值得注意的是,{regex:/Cancelled/i}
只是用键 regex
和字符串值 /Cancelled/i
(包括斜线)创建一个新对象,这显然不等于 $CaseClientStatus
中的任何内容。这是一个解决方案:
ClientStatus:{
$switch: {
branches: [
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /In Progress/i}}]
}, then:'In Progress'},
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /Cancelled/i}}]
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
看起来您正在尝试采用某种形式自由的状态“描述”字段并从中创建强大的枚举状态。我建议您的 $ClientStatus
输出更像代码,例如IN_PROGRESS, COMPLETE, CXL
等等。消除大小写,当然还有空格。