0

I am trying to use $in in MongoDB query similar to this one

This query works for me and uses { $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" }
                   }
               }
          }
       ]
    )

However when I use {tweet:{$in:[/tesla/i,/rocket/,"coffee"]}} in almost the same query I get aggregation failed error

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" }
               }
           }
      }
   ]
)

I know that in a simpler query the $in works

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

However I am not understanding the correct way to format $in aggregation method https://docs.mongodb.com/manual/reference/operator/aggregation/in/

I have tried swapping with { $in: [ "tesla", "$tweet" ] } but this still creates aggregation failed error

db.Tweets.aggregate(
   [
      {
         $project:
           {
             _id: 0,
            tweet: 1,
             convert:
               {
                 $cond: { if: 

{
        $in: [ "tesla", "$tweet" ]
      }

, then: "contains words", else: "does not contain words" }
               }
           }
      }
   ]
)

Please help. I am still new to MongoDB and curly braces, the queries look almost the same to me apart from using $in and the $gte.

2
  • Please provide sample input documents - and of course the error your get. "I get aggregation failed error" is not really helpful. Commented Nov 27, 2021 at 21:44
  • /tesla/i,/rocket/ are regular expressions. You need plain strings Commented Nov 27, 2021 at 21:46

1 Answer 1

1

It is mentioned in the documentation you provided here: $in agg docs

" Unlike the $in query operator, the aggregation $in operator does not support matching by regular expressions. "

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.