Let's say we have two documents in below collection in 3.4.4-7-g70415a0.
db.testcol.insertMany([{ "name" : "first", "value" : [ 1, 2, 3 ] }, { "name" : "second", "value" : [ 4, 5, 6 ] }]);
Using $in with $addFields for input array [1,2,3]
Running below query
db.testcol.aggregate([{
$addFields: {
cmp: { $in: [ {
$zip: {
inputs: [[1, 2, 3], {
$range: [0, {
$size: {$literal:[1, 2, 3]}
}]
}]
}
}, {
$zip: {
inputs: ["$value", {
$range: [0, {
$size: "$value"
}]
}]
}
}] }
}
}])
returns
{ "_id" : ObjectId("591724e9f3b93cb449118cd3"), "name" : "first", "value" : [ 1, 2, 3 ], "cmp" : false }
{ "_id" : ObjectId("591724e9f3b93cb449118cd4"), "name" : "second", "value" : [ 4, 5, 6 ], "cmp" : false }
Using $match with input array and its index [1, 0], [2, 1], [3, 2]
Running below query
db.testcol.aggregate([{
$addFields: {
valueWithIx: {
$zip: {
inputs: ["$value", {
$range: [0, {
$size: "$value"
}]
}]
}
},
}
}, {
$match: {
valueWithIx: {
$in: [
[1, 0],
[2, 1],
[3, 2]
]
}
}
}])
returns
{ "_id" : ObjectId("591724e9f3b93cb449118cd3"), "name" : "first", "value" : [ 1, 2, 3 ], "valueWithIx" : [ [ 1, 0 ], [ 2, 1 ], [ 3, 2 ] ] }
Can someone help understand this behavior ? I'm expecting the first query to return value "cmp" : true for document with "name":first
Appreciate any help.