【问题标题】:$concatArrays only supports arrays, not object$concatArrays 只支持数组,不支持对象
【发布时间】:2020-11-24 19:51:36
【问题描述】:

我正在尝试在 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 新手,所以我很确定我在某处错过了数组的概念。

【问题讨论】:

    标签: arrays mongodb go slice mongo-go


    【解决方案1】:

    您为 initialValue 提供的 Go 值不是数组:

    "initialValue": bson.M{},
    

    改为:

    "initialValue": []interface{}{},
    

    或者:

    "initialValue": bson.A{},
    

    【讨论】:

    • 行了!我只是看错地方了。非常感谢!
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2017-05-20
    • 2021-03-17
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    相关资源
    最近更新 更多