無法獲得所需的輸出。出現“無法識別的運算式'$regex'”錯誤
[
{
'$lookup': {
'from': 'profiles',
'let': {
'userId': '$userId',
},
'pipeline': [
{
'$match': {
$expr: {
$and: [
{ '$eq': ['$uniqueId', '$$mcontactId'] },
{
$or: [{ 'birthDate': { '$regex': '$$today' } },
{ 'spouseBirthdate': { '$regex': '$$today' } },
{ 'weddingAnniversary': { '$regex': '$$today' } },
],
},
],
},
},
},
{
'$project': {
'userId': 1,
'uniqueId': 1,
'mobileNumber': 1,
'whatsApp': 1,
'emailId': 1,
'lastName': 1,
'firstName': 1,
'address': 1,
'signature': 1,
},
},
],
'as': 'profile',
},
},
]
uj5u.com熱心網友回復:
$regex是一個查詢運算子,您試圖在$expr中使用它,它使用“聚合”語言,而不是通常在$match階段中使用的“查詢”語言。
除此之外,您的管道中還有一些其他問題,例如,您僅定義$userId為$lookup階段的變數,但您正在嘗試使用其中$$today并$$mcontactId沒有在任何地方定義。
無論如何,一旦您解決了這些問題,您都有兩個選擇:
- 如果正則運算式匹配與輸入變數無關,請在
$regex外部使用$expr,如下所示:
{
'$match': {
$and: [
{
$expr: {
'$eq': [
'$uniqueId',
'$$userId',
],
},
},
{
$or: [
{
'birthDate': {
'$regex': '03-05',
},
},
],
},
],
},
},
蒙戈游樂場
- 如果正則運算式不使用來自 的輸入變數,
$lookup那么您需要使用聚合運算子,例如$regexMatch在$expr
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453381.html
