dsfphczao23473056
dsfphczao23473056
2019-05-25 15:22
采纳率: 100%
浏览 383

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

假如有这样一个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驱动程序mongogo更新第一人生成就(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”和“作用域”字段指定的元素。可能是我不恰当地使用arrayFilters,也可能是我不正确地设置了FindOneandUpdate函数的Param。导致现在它不会抛出任何错误,也不会更新文档。请帮帮我吧!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhila3786
    dongzhila3786 2019-05-26 03:42
    已采纳

    try this:

    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}},
        }),
    )
    

    which "translates" to shell:

    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 } ]}
    )
    
    点赞 评论
  • dougou7782
    dougou7782 2019-05-26 12:06

    So, with help of @owlwalks, we have found the decision:

    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),
        )
    
    点赞 评论

相关推荐