在MongoDB条件聚合查询中使用$in聚合失败



我正试图在MongoDB查询中使用$in,类似于这个

此查询适用于我,并使用{$gte:["$likes_count",1000]}

db.Tweets.aggregate(
[
{
$project:
{
_id: 0,
tweet: 1,
convert:
{
$cond: { if: 
{ $gte: [ "$likes_count", 1000 ] }
, then: "1000 likes or more", else: "less than 1000 likes" }
}
}
}
]
)

然而,当我在几乎相同的查询中使用{tweet:{$in:[/tesla/I,/rocket/,"coffee"]}}时,我得到聚合失败错误

db.Tweets.aggregate(
[
{
$project:
{
_id: 0,
tweet: 1,
convert:
{
$cond: { if: 
{tweet:{$in:[/tesla/i,/rocket/,"coffee"]}}
, then: "contains words", else: "does not contain words" }
}
}
}
]
)

我知道在一个更简单的查询中$in工作

数据库。Tweets.find({tweet:{$in:[/tesla/i,/rocket/,"coffee"]}},{likes_count:1,tweet:1,_id:0}(

但是,我不理解在聚合方法中格式化$的正确方法https://docs.mongodb.com/manual/reference/operator/aggregation/in/

我尝试过用{$in:["tesla","$tweet"]}进行交换,但这仍然会导致聚合失败错误

db.Tweets.aggregate(
[
{
$project:
{
_id: 0,
tweet: 1,
convert:
{
$cond: { if: 
{
$in: [ "tesla", "$tweet" ]
}
, then: "contains words", else: "does not contain words" }
}
}
}
]
)

请帮忙。我还是MongoDB和花括号的新手,除了使用$in和$gte之外,查询对我来说几乎是一样的。

您在此处提供的文档中提到了这一点:$in agg文档

"与$in查询运算符不同,聚合$in运算符不支持正则表达式匹配。">

最新更新