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

2024年 2月 10日 91.5k 0

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

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)。

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

{
: { : , ... },
: { : , ... },
...
}

登录后复制

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

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

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

登录后复制

以上就是如何使用内部数组中的值更新文档的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论