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

标签 arrays mongodb go slice mongo-go

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

相关文章:

javascript - 在 mongoDB 中,如何使用 async/await 获取使用 insertOne() 创建的对象的 id?

php - 在 Laradock 应用程序中找不到类 'MongoId'

go - 将数据范围传递给 HTML 模板

arrays - 如何检查直线多边形是否相交

javascript - Odd Ones Out - 创建一个在原始数组中出现偶数次的项目数组

javascript - React Native 设置对象数组的元素状态

javascript - find() 和 findById() 在一个 MongoDB 调用中

json - 不要将不需要的 JSON 键值读入内存

datetime - 将 UTC 字符串转换为时间对象

javascript - Eloquent Javascript 练习 5.2。帮助整理思绪