Mongo-go如何使用arrayFilter在“对象数组内的对象数组”中查找元素

时间:2019-05-25 15:22:46

标签: mongodb go mgo mongo-go

让我们想象一个像这样的JSON:

"user": {
  "id": "1234",
  ...some fields,
  "achievements": [
    {
      "scope": "life achievements",
      "list": [
        {"_id": 1, "title": "some text", "gotAt": "some date"},
        {"_id": 2, "title": "some other text", "gotAt": "some date"}
      ]
    },
    {
      "scope": "sport achievements",
      "list": [
        {"_id": 1, "title": "sport", "gotAt": "some date"},
        {"_id": 2, "title": "some other sport", "gotAt": "some date"}
      ]
    },
    {
      "scope": "academic achievements",
      "list": [
        {"_id": 1, "title": "some text", "gotAt": "some date"},
        {"_id": 2, "title": "some other text", "gotAt": "some date"}
      ]
    },
  ]
}

例如,我需要使用mongoDb和Golang驱动程序Mongo-go更新第一个人生成就(ID为1)

问题出在嵌套层。我可以通过$ mongoDb运算符获取一些数组元素,但是在这里,我需要通过活动范围(生活,体育,学术等)和成就的_id字段来识别每个用户的成就。因此,我需要在其他对象数组内的对象数组内进行搜索。

那是我完成的事情(反正还是行不通的)

doc := coll.FindOneAndUpdate(
        context.Background(),
        bson.D{
            {"_id", "1234"},
            {"achievements.scope", "life achievements"},
            {"achievements.list._id", 1},
        },
        bson.D{
            {"$set", bson.D{
                {"achievements.$.list.$[elem].title", "some new test"},
            }},
        },
        options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
            Filters: append(make([]interface{}, 0), bson.D{
                {"elem", bson.D{
                    {"achievements.list._id", 1},
                }},
            }),
        }).SetReturnDocument(1),
    )

我希望此查询仅更新“ id”和“ scope”字段指定的一个元素。可能是我不正确地使用了arrayFilters,或者是我未正确设置FindOneAndUpdate函数的参数。现在,它不会引发任何错误,也不会更新文档。请帮助我

2 个答案:

答案 0 :(得分:0)

尝试一下:

mongo-go-driver:

coll.FindOneAndUpdate(
    context.Background(),
    bson.D{
        {"id", "1234"},
        {"achievements.scope", "life achievements"},
        {"achievements.list._id", 1},
    },
    bson.M{"$set": bson.M{"achievements.$.list.$[elem].title"": "some new test"}},
    options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
        Filters: []interface{}{bson.M{"elem._id": 1}},
    }),
)

“翻译”为外壳:

db.user.findOneAndUpdate(
  {"id": "1234","achievements.scope": "life achievements","achievements.list._id": 1},
  {$set: {"achievements.$.list.$[elem].title": "some new test"}},
  { arrayFilters: [  { "elem._id": 1 } ]}
)

答案 1 :(得分:0)

因此,在@owlwalks的帮助下,我们找到了决定:

doc := coll.FindOneAndUpdate(
        context.Background(),
        bson.D{
            {"_id", "1234"},
            {"achievements.scope", "life achievements"},
        },
        bson.D{
            {"$set", bson.D{
                {"achievements.$.list.$[elem].title", "some new text"},
            }},
        },
        options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{
            Filters: []interface{}{bson.D{
                    {"elem._id", 1},
                }},
        }).SetReturnDocument(1),
    )