更新存在的数组或插入新的数组项 [英] Update Array where it exists or Insert new Array Item

查看:78
本文介绍了更新存在的数组或插入新的数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
 _id:'1',
name:'apple',
option:[{
weight:'10',
price:'40',
},
{weight:'40',
price:'200'}]
}

如果苹果不在数据库中,我想添加苹果,如果在数据库中,我想添加重量10的苹果,如果可用,我要更新价格,否则,我想更新价格,我想在选项中添加新的重量和价格.在mongodb中,我该怎么办.

I want to add apple if its not in db, if its in db, I want to ckeck if weight 10 is available, if available I want to update the price if not I want to add new weight and price in option. How can I do it, in mongodb.

推荐答案

您要 $set 编写更新,其中数据会存在或 $push 不存在的新数据:

You want .bulkWrite() for this. This is not actually a single operation, so you want to submit multiple operations in a single request. Essentially attempt to write the update with $set where data does exist or $push the new data where it does not exist:

db.collection.bulkWrite([
  { "updateOne": {
    "filter": { "_id": "1", "option.weight": "10" },
    "update": { 
      "$set": { "option.$.price": "30" }
    }
  }},
  { "updateOne": {
    "filter": { "_id": "1", "option.weight": { "$ne": "10" } },
    "update": {
      "$push": { "option": { "weight": "10", "price": "30" } }
    }
  }}
])

肯定的情况就是值,而 $ne 否定"相等匹配项,表示该项目不存在.当然位置$运算符 $set

The positive case is simply the value, and the $ne "negates" the equality match, meaning the item does not exist. Of course the positional $ operator is used with $set where it does

鉴于数据,尽管批"中发送了两个操作,但实际上只有一个操作会匹配并作为更新应用.

Given the data only one of the operations will actually match and apply as an update despite two operations being sent in the "batch".

如果您还想要整个文档的"upserts",那么您需要在该文档的末尾添加另一个操作.请注意,您不能在其他任何一条语句上都应用"upsert"作为选项,尤其是

If you want "upserts" for the whole document as well, then you need to add another operation to the end of that. Note that you cannot apply "upsert" as an option on either of the other statements, especially the $ne because that would create a new document where the array item does not exist, not just the _id:

db.collection.bulkWrite([
  { "updateOne": {
    "filter": { "_id": "1", "option.weight": "10" },
    "update": { 
      "$set": { "option.$.price": "30" }
    }
  }},
  { "updateOne": {
    "filter": { "_id": "1", "option.weight": { "$ne": "10" } },
    "update": {
      "$push": { "option": { "weight": "10", "price": "30" } }
    }
  }},
  { "updateOne": {
    "filter": { "_id": 1 },
    "update": {
      "$setOnInsert": {
        "option": [
           { "weight": "10", "price": "30" }
         ]
      }
    },
    "upsert": true
  }}
])

$setOnInsert 是这里的主要帮助从最后一个操作开始,唯一标记为"upsert".这种组合可确保在找到主要文档"的位置上没有任何实际发生,但是在找不到该位置时,将添加新的数组项.

The $setOnInsert is the main help here apart from that last operation being the only marked as "upsert". That combination makes sure that where the primary "document" is found then nothing actually happens, but when it's not found the new array item is added.

作为一个旁注,我强烈建议将数字值实际存储为数字而不是字符串.在大多数情况下,它不仅可以节省空间,而且这种方式也非常有用.

As a side note, I would strongly suggest storing numeric values actually as numeric rather than strings. Not only does it save on space in most cases but it's also far more useful that way.

这篇关于更新存在的数组或插入新的数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆