登录
首页 >  Golang >  Go问答

如何在嵌套的数组对象中使用arrayFilter来查找元素

来源:stackoverflow

时间:2024-03-15 08:33:27 381浏览 收藏

今天golang学习网给大家带来了《如何在嵌套的数组对象中使用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 驱动程序 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 函数的参数。现在它不会抛出任何错误,也不会更新文档。请帮助我


解决方案


使用 go.mongodb.org/mongo-driver/mongo 包,您可以这样做:

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

“翻译”为 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}]}
)

因此,在 @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),
    )

今天关于《如何在嵌套的数组对象中使用arrayFilter来查找元素》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>