regex - mongodb聚合查找管道正则表达式匹配
问题描述
我正在尝试在聚合管道中进行正则表达式匹配$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
但是它不会嵌套在管道中。
这是一个错误还是我忽略了什么?还有另一种方法可以进行这种正则表达式匹配吗?
解决方案
显然,问题似乎是我试图在$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/]
}
}
}
]
}
如果有人可以详细说明为什么会这样,请随意
推荐阅读
- ipython - 为什么在 ipython 上的 if 块之后续行会立即中断?
- windows - 打包为 UWP 应用时,带有两个字母名称 ('bg') 的文件夹内的图像消失
- ember.js - ember 引擎 routes.js 中的嵌套路由?
- jquery - Handsontable 中的虚拟键盘
- assembly - EAFFFFFE 在 ARM 架构中是什么意思
- php - 执行 mysqli_query 有效,但它不返回 WHERE CLAUSE 中给出的确切行
- javascript - 全球 Firebase 参考
- ios - AVCaptureSession captureOutput 回调丢帧并抛出 OutOfBuffers 错误
- android - 使用 Appium 与 Uiautomator 的优势
- c - C:没有单独的函数,代码就无法工作。为什么?