Combine $in with $split in mongo
This "fail" is the expected behavior, let's understand why.
We must first take a look at the $match behavior as specified in the docs:
$match takes a document that specifies the query conditions. The query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. Instead, use a $expr query expression to include aggregation expression in $match.
This means when you use $match
it uses the query language by default, now the "issue" comes from the difference between the two $in
operators the query $in operator (which is being used) and the aggregation $in operator ( which you assume is being used ).
It is true that $split
resolves to an array. but $split
is also an aggregation operator, now I think this case should throw an error but for some reason as you mentioned this behavior just resolves with no results. the aggregation $in
operator however , does accept raw aggregation expressions.
This means all you have to do is convert your $match
query to use $expr so you can use the aggregation version of $in
within the match, like so:
db.collection.aggregate([
{
"$match": {
$expr: {
$in: [
"$myid",
{
"$split": [
"136618,136620,136622",
","
]
}
]
}
}
}
])
Mongo Playground