【问题标题】:Aggregation failed using $in in MongoDB conditional aggregation query在 MongoDB 条件聚合查询中使用 $in 聚合失败
【发布时间】:2021-12-01 02:03:01
【问题描述】:

我正在尝试在类似于此的 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 有效

db.Tweets.find({tweet:{$in:[/tesla/i,/rocket/,"coffee"]}} , {likes_count:1, tweet:1, _id:0})

但是我不理解格式化 $in 聚合方法的正确方法 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 之外,查询对我来说几乎是一样的。

【问题讨论】:

  • 请提供样本输入文件——当然还有你得到的错误。 “我得到聚合失败错误”并不是很有帮助。
  • /tesla/i,/rocket/ 是正则表达式。你需要纯字符串

标签: mongodb


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2019-06-18
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多