如何更新文档数组中的对象(嵌套更新)

编程入门 行业动态 更新时间:2024-10-22 18:32:30
本文介绍了如何更新文档数组中的对象(嵌套更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我们有以下集合,我对此有一些疑问:

Assume we have the following collection, which I have few questions about:

{ "_id" : ObjectId("4faaba123412d654fe83hg876"), "user_id" : 123456, "total" : 100, "items" : [ { "item_name" : "my_item_one", "price" : 20 }, { "item_name" : "my_item_two", "price" : 50 }, { "item_name" : "my_item_three", "price" : 30 } ] }

  • 我想提高item_name"的价格:my_item_two"如果它不存在,则应将其附加到items"数组.

  • I want to increase the price for "item_name":"my_item_two" and if it doesn't exists, it should be appended to the "items" array.

    如何同时更新两个字段?例如,提高my_item_three"的价格;同时增加总数"(具有相同的值).

    How can I update two fields at the same time? For example, increase the price for "my_item_three" and at the same time increase the "total" (with the same value).

    我更喜欢在 MongoDB 端执行此操作,否则我必须在客户端 (Python) 中加载文档并构建更新的文档并将其替换为 MongoDB 中的现有文档.

    I prefer to do this on the MongoDB side, otherwise I have to load the document in client-side (Python) and construct the updated document and replace it with the existing one in MongoDB.

    这是我尝试过的并且工作正常如果对象存在:

    This is what I have tried and works fine if the object exists:

    db.test_invoice.update({user_id : 123456 , "items.item_name":"my_item_one"} , {$inc: {"items.$.price": 10}})

    但是,如果密钥不存在,则它什么也不做.此外,它只更新嵌套对象.此命令无法更新总数".领域也是如此.

    However, if the key doesn't exist, it does nothing. Also, it only updates the nested object. There is no way with this command to update the "total" field as well.

    推荐答案

    对于问题 #1,让我们将其分为两部分.首先,增加items.item_name"等于my_item_two"的任何文档.为此,您必须使用位置$"运算符.类似的东西:

    For question #1, let's break it into two parts. First, increment any document that has "items.item_name" equal to "my_item_two". For this you'll have to use the positional "$" operator. Something like:

    db.bar.update( {user_id : 123456 , "items.item_name" : "my_item_two" } , {$inc : {"items.$.price" : 1} } , false , true);

    请注意,这只会增加任何数组中第一个匹配的子文档(因此,如果数组中有另一个文档item_name"等于my_item_two",则不会增加).但这可能是您想要的.

    Note that this will only increment the first matched subdocument in any array (so if you have another document in the array with "item_name" equal to "my_item_two", it won't get incremented). But this might be what you want.

    第二部分比较棘手.我们可以将一个新项目推送到一个没有my_item_two"的数组,如下所示:

    The second part is trickier. We can push a new item to an array without a "my_item_two" as follows:

    db.bar.update( {user_id : 123456, "items.item_name" : {$ne : "my_item_two" }} , {$addToSet : {"items" : {'item_name' : "my_item_two" , 'price' : 1 }} } , false , true);

    对于您的问题 #2,答案更简单.要在包含my_item_three"的任何文档中增加 item_three 的总数和价格,您可以同时在多个字段上使用 $inc 运算符.类似的东西:

    For your question #2, the answer is easier. To increment the total and the price of item_three in any document that contains "my_item_three," you can use the $inc operator on multiple fields at the same time. Something like:

    db.bar.update( {"items.item_name" : {$ne : "my_item_three" }} , {$inc : {total : 1 , "items.$.price" : 1}} , false , true);
  • 更多推荐

    如何更新文档数组中的对象(嵌套更新)

    本文发布于:2023-11-28 10:10:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1641979.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:嵌套   组中   对象   文档

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!