我试图使用Moongoose(NestJS Wrapper)在MongoDB中聚合对象,并遇到了过滤数据的问题。我有一个字段profileID,不同的集合使用它来链接对象。我正在使用match运算符基于多个属性进行过滤,似乎无法弄清楚如何执行以下操作:
通过检查profileID是否不在多个不同的数组中来过滤profileID,这是我到目前为止已经尝试过的(适应了另一篇文章的答案)
return this.userProfile.aggregate(
[{ $match: {
...
profileId: {
$or: [
{$nin: userData.blocked},
{$nin: userData.matches},
{$nin: userData.rejects},
{$nin: userData.unmatched}]
}
...
}
我现在收到以下错误:
[Nest] 20356 - 12/24/2019, 22:42:34 [ExceptionsHandler] unknown operator: $or +77161ms
MongoError: unknown operator: $or
[如果我尝试让个人不在多个profileID字段中,也会出现错误
TS1117: An object literal cannot have multiple properties with the same name in strict mode.
TS2300: Duplicate identifier 'profileID'.
$ or是顶级命令,因此您需要做更多类似的事情...
return this.userProfile.aggregate(
[{ $match: {
...
$or: [
{ profileId: {$nin: userData.blocked}},
{ profileId: {$nin: userData.matches}},
{ profileId: {$nin: userData.rejects}},
{ profileId: {$nin: userData.unmatched}}
]
}
...
}
如果您确实想拥有单独的$ nin条件,则以我为例。相反,最好仅在$ nin上将所有值组合在一个数组中(这样就不需要$ or了),就像这样]
return this.userProfile.aggregate(
[{ $match: {
...
profileId: {
$nin: [...userData.blocked, ...userData.matches, ...userData.rejects, ...userData.unmatched]
}
...
}