英文:
mongodb aggregation lookup pipeline regex match
问题
我正在尝试在聚合管道$lookup
中进行正则表达式匹配。
所以假设以下查询:
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
}
]
}
}
}
]
}
这一切都运行得很顺利,我可以根据多个条件进行匹配,它可以正常工作。
但是,如果我想使用正则表达式数组添加另一个条件,我无法让它工作:
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
},
{
$in: ['$some-type', [/type1/, /type2/]]
}
]
}
}
}
]
}
为什么这不起作用?根据文档,我应该能够在$in
运算符内部以这种方式使用正则表达式,并且我可以确认它有效,因为我们在其他地方使用它。但在$lookup
管道内部嵌套时不起作用。
这是一个错误吗,还是我忽略了什么?是否有其他方法可以执行这种类型的正则表达式匹配?
英文:
I'm trying to do a regex match inside an aggregation pipeline $lookup
So lets assume the following query:
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
}
]
}
}
}
]
}
This all works great, i can match on multiple conditions, and it works.
However if i want to add another condition using an array of regex i can't get it to work
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
},
{
$in: ['$some-type', [/type1/, /type2/]]
}
]
}
}
}
]
}
Why does this not work? as i understand it from the documentation i should be able to use regex this way inside an $in
operator, and i can confirm that it works, since we use it elsewhere. However nested within a $lookup
pipeline it does not.
Is this a bug or am i overlooking something? Is there another way i can do this kind of regex match?
答案1
得分: 1
显然,问题似乎是我试图在$expr
运算符内进行正则表达式匹配,我不确定为什么它不起作用,我在文档中找不到任何相关信息。
但是,将它移到管道内的单独匹配中就可以了。
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
}
]
}
}
},
{
$match: {
some-type: {
$in: [/type1/, /type2/]
}
}
}
]
}
如果有人能详细解释为什么会这样,欢迎分享。
英文:
Evidently, the problem appears to be that i was attempting to regex match inside the $expr
operator, im unsure as to why it does not work, and i can't find anything within the documentation.
But by moving it to a seperate match within the pipeline it worked.
$lookup: {
from: 'some-collection',
let: {
someIds: '$someIds'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$in: ['$someId', '$$someIds']
},
{
$not: {
$eq: ['$status', 'archived']
}
}
]
}
}
},
{
$match: {
some-type: {
$in: [/type1/, /type2/]
}
}
}
]
}
If anyone can elaborate on why this is the case feel free
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论