I'm trying to code a perfectly working MongoDB query in Go, but I'm having a hard time with arrays.
Working on JSON:
[
...
{
$project: {
acl: {
$reduce: {
input: "$a.accesses",
initialValue: [],
in: {
$concatArrays: ["$$value", "$$this"]
}
}
}
}
}]
But not working on Go:
pipe := mongo.Pipeline{
...
bson.D{{Key: "$project", Value: bson.M{
"acl": bson.M{
"$reduce": bson.M{
"input": "$a.accesses",
"initialValue": bson.M{},
// None of the below works
"in": bson.M{"$concatArrays": bson.A{"$$value", "$$this"}},
// "in": bson.M{"$concatArrays": []interface{}{"$$value", "$$this"}},
// "in": bson.M{"$concatArrays": [2]string{"$$value", "$$this"}},
// "in": bson.M{"$concatArrays": []string{"$$value", "$$this"}},
// "in": bson.M{"$concatArrays": []interface{}{"$$value", "$$this"}},
// "in": bson.D{{Key: "$concatArrays", Value: []interface{}{"$$value", "$$this"}}},
},
},
}}},
}
Error: $concatArrays only supports arrays, not object
I'm new in Go so I'm quite sure I'm missing the concept of arrays somewhere.
The Go value you supply for initialValue
is not an array:
"initialValue": bson.M{},
Instead do:
"initialValue": []interface{}{},
Or:
"initialValue": bson.A{},