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

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

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

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

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 编写,开头如下:

Calliper 文档对比神器
Calliper 文档对比神器

文档内容对比神器

Calliper 文档对比神器28
查看详情 Calliper 文档对比神器
// 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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号