Hello,
I stumbled into this yesterday and couldn’t find anything about it online. An identical aggregation became much faster than switching the object key to array. Examples:
Slow:
[
{
$group: {
_id: { Field1: "$Field1", Field2: "$Field2" },
Field1: {
$first: "$Field1",
},
Field2: {
$first: "$Field2",
},
Field3: {
$push: {
Value: "$Value",
Date: "$Date",
},
},
},
},
]
Fast:
[
{
$group: {
_id: ["$Field1", "$Field2"],
Field1: {
$first: "$Field1",
},
Field2: {
$first: "$Field2",
},
Field3: {
$push: {
Value: "$Value",
Date: "$Date",
},
},
},
},
]
Any clues as to why this is? I assume it has something to do with how MongoDB treats the keys internally and I also noticed that the execution plan changed (the one with object using slot-based, the other not).
I haven’t found any examples using an array online, but it yields an identical resultset in our case, just much faster.
Thanks!