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.
/tesla/i,/rocket/are regular expressions. You need plain strings