首页 > 后端开发 > Golang > 正文

如何使用内部数组中的值更新文档

王林
发布: 2024-02-10 11:09:09
转载
862 人浏览过

如何使用内部数组中的值更新文档

php小编香蕉为你带来了一篇关于如何使用内部数组中的值更新文档的实用指南。在开发过程中,我们经常需要从一个数组中获取数据并将其更新到文档中。本文将介绍使用PHP的内部数组中的值来更新文档的方法,这种方法简单而灵活,能够帮助我们更高效地处理数据更新任务。无论你是初学者还是有经验的开发者,希望本文能为你带来一些有价值的知识和技巧。让我们马上开始吧!

问题内容

我被困在一些看起来并不复杂的事情上,也许有些东西是我没有想到或没有看到的。

拥有(很多)包含对象数组的文档,例如:

{
    "_id": "ox1",
    "results": [
        {
            "id": "a1",
            "somethingelse": "aa",
            "value": 1
        },
        {
            "id": "a2",
            "somethingelse": "bb",
            "value": 2
        },
        {
            "id": "a3",
            "somethingelse": "cc",
            "value": 3
        }
    ],
    "total": 0
},
{
    "_id": "ox2",
    "results": [
        {
            "id": "a1",
            "somethingelse": "aa",
            "value": 44
        },
        {
            "id": "a4",
            "somethingelse": "bb",
            "value": 4
        },
        {
            "id": "a5",
            "somethingelse": "aa",
            "value": 5
        }
    ],
    "total": 0
},
{
    "_id": "ox3",
    "results": [
        {
            "id": "a2",
            "somethingelse": "aa",
            "value": 1
        },
        {
            "id": "a3",
            "somethingelse": "aa",
            "value": 4
        },
        {
            "id": "a4",
            "somethingelse": "aa",
            "value": 5
        }
    ],
    "total": 0
}
登录后复制

我想要一个 updatemany 查询来更新所有具有“结果”的文档,其中包含:

  • “id”:“a1”
  • "somethingelse": "aa"

通过包含“id”:“a1”和“somethingelse”:“aa”的“结果”的值增加其“总计”

所以在我们的例子中: “0x1”的结果包含“id”:“a1”和“somethingelse”:“aa”的“值”为 1 -> 我希望它的“总数”增加1

“0x2”的结果包含“id”:“a1”和“somethingelse”:“aa”的“值”为 44 -> 我希望它的“总数”增加 44

“0x3”不满足条件

用 go 编写,开头如下:

// Here I filter only the documents meeting the condition
filter := bson.D{{
    Key: "results,
    Value: bson.D{{
        Key: "$elemMatch",
        Value: bson.D{
            {Key: "id", Value: "a1"},
            {Key: "somethingElse", Value: "aa"},
        }},
    }},
}

// This is where it gets tricky
addTotal := bson.M{
    "$set": bson.D{{
        Key: "total",
        Value: bson.D{{
            Key: "$sum",
            Value: bson.A{
                "$total",
                bson.M{ 
                    // How can I get the "value" from the right object from the array ?
                },
            },
        }},
    }},
}
登录后复制

这可能吗? 我还没有找到太多关于内部/嵌入式查询的信息。

解决方法

db.collection.updatemany(filter, update, options) 中的 update 参数可以是更新文档或聚合管道 (doc)。

更新文档仅包含更新运算符表达式,看起来像这样:

{
   <operator1>: { <field1>: <value1>, ... },
   <operator2>: { <field2>: <value2>, ... },
   ...
}
登录后复制

值不能引用文档中的字段。

虽然聚合管道更先进,可以引用文档中的字段。这是使用聚合管道执行此操作的一种方法:

db.collection.updatemany(
  { results: { $elemmatch: { id: 'a1', somethingelse: 'aa' } } },
  [
    {
      $set: {
        total: {
          $let: {
            vars: {
              items: {
                $filter: {
                  input: '$results',
                  as: 'item',
                  cond: {
                    $and: [
                      { $eq: ['$$item.id', 'a1'] },
                      { $eq: ['$$item.somethingelse', 'aa'] },
                    ],
                  },
                },
              },
            },
            in: { $add: ['$total', { $sum: '$$items.value' }] },
          },
        },
      },
    },
  ]
);
登录后复制

翻译成go代码:

filter := bson.M{
    "results": bson.M{
        "$elemMatch": bson.M{
            "id":            "a1",
            "somethingElse": "aa",
        },
    },
}

vars := bson.M{
    "items": bson.M{
        "$filter": bson.M{
            "input": "$results",
            "as":    "item",
            "cond": bson.M{
                "$and": bson.A{
                    bson.M{"$eq": bson.A{"$$item.id", "a1"}},
                    bson.M{"$eq": bson.A{"$$item.somethingElse", "aa"}},
                },
            },
        },
    },
}

update := bson.A{
    bson.M{
        "$set": bson.M{
            "total": bson.M{
                "$let": bson.M{
                    "vars": vars,
                    "in": bson.M{
                        "$add": bson.A{"$total", bson.M{"$sum": "$$items.value"}},
                    },
                },
            },
        },
    },
}
登录后复制

以上是如何使用内部数组中的值更新文档的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!