gpt4 book ai didi

arrays - $ concatArrays仅支持数组,不支持对象

转载 作者:行者123 更新时间:2023-12-01 21:09:33 25 4
gpt4 key购买 nike

我正在尝试在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com