我正在尝试在Go中编写一个可以正常运行的MongoDB查询,但是我在使用数组时遇到了困难。
使用JSON:
[
...
{
$project: {
acl: {
$reduce: {
input: "$a.accesses",
initialValue: [],
in: {
$concatArrays: ["$$value", "$$this"]
}
}
}
}
}]
但不能在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"}}},
},
},
}}},
}
错误:$concatArrays only supports arrays, not object
我是Go语言的新手,所以我很确定自己在某个地方缺少数组的概念。
最佳答案
您为initialValue
提供的Go值是而不是一个数组:
"initialValue": bson.M{},
而是:"initialValue": []interface{}{},
或者:"initialValue": bson.A{},
关于arrays - $ concatArrays仅支持数组,不支持对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63251586/