I tried this below but got: { MongoServerError: PlanExecutor error during aggregation :: caused by :: $concat only supports strings, not objectId and I don’t know what to do
'$clientId' is probably an ObjectId. If so, use $toString, as { $toString: "$clientId" }.
Provide some example documents and expected outcome. You can also use Mongo Playground to create a reproducible example.
Btw, to create a unique _id for $group with multiple fields, you don’t have to concatenate the different fields. You can directly use each of the fields, like this:
{
$group: {
_id: {
// multiple fields
periodPart: { $substr: ["$period", 0, groupByLength[options.groupBy]] },
clientId: "$clientId", // no need to convert clientId to a string
endpoint: "$endpoint"
},
count: { $sum: "$count" },
points: { $avg: { $multiply: ["$count", "$points"] } }
}
}