我有这样的结构
unions { // collection
members { // array
instanceId // some id
...
}
...
}在文档中,我有id支柱(数组),我需要查找至少有一个id的所有联合(基本上是$in)。
问题是它不起作用
首先,我想试试这个变体。
{
from: 'unions',
let: { instanceIds: '$ids' },
as: 'unions',
pipeline: [
{
$match: { 'members.instanceId': { $in: '$$instanceIds' } },
},
],
}但是我们不能在这里使用聚合变量。为此,我们需要使用$expr
{
from: 'unions',
let: { instanceIds: '$ids' },
as: 'unions',
pipeline: [
{
$match: {
$expr: {
$in: ['$members.instanceId', '$$instanceIds']
}
},
},
],
}但是它会返回0份文档。instanceIds数组不是空的,我已经检查过了。另外,如果我在示例中粘贴一个没有$expr的值的数组,那么它将返回正确的值。所以最有可能的问题是我是如何构建这个$lookup的。
发布于 2020-07-21 14:50:17
使用{ $ne:[{ $setIntersection:‘$embers.instanceId’,'$$instanceIds‘},[]] }}
{
from: 'unions',
let: { instanceIds: '$ids' },
as: 'unions',
pipeline: [
{
$match: {
$expr: {
cond: { $ne: [{ $setIntersection: ['$members.instanceId', '$$instanceIds'] }, []] },
},
},
},
],
}https://stackoverflow.com/questions/63016316
复制相似问题