【问题标题】:Update field in exact element array in MongoDB更新 MongoDB 中精确元素数组中的字段
【发布时间】:2012-05-13 01:04:21
【问题描述】:

我的文档结构如下:

{
    _id:"43434",
    heroes : [
        { nickname : "test",  items : ["", "", ""] },
        { nickname : "test2", items : ["", "", ""] },
    ]
}

我可以$setheros数组中嵌入对象的items数组的第二个元素nickname"test"吗?

结果:

{
    _id:"43434",
    heroes : [
        { nickname : "test",  items : ["", "new_value", ""] }, // modified here
        { nickname : "test2", items : ["", "", ""] },
    ]
}

【问题讨论】:

    标签: mongodb mongodb-query nosql


    【解决方案1】:

    您需要使用 2 个概念:mongodb's positional operator,并且只需使用要更新的条目的数字索引。

    位置运算符允许您使用这样的条件:

    {"heroes.nickname": "test"}
    

    然后像这样引用找到的数组条目:

    {"heroes.$  // <- the dollar represents the first matching array key index
    

    当您要更新“项目”中的第二个数组条目时,数组键的索引为 0 - 这就是键 1。

    所以:

    > db.denis.insert({_id:"43434", heroes : [{ nickname : "test",  items : ["", "", ""] }, { nickname : "test2", items : ["", "", ""] }]});
    > db.denis.update(
        {"heroes.nickname": "test"}, 
        {$set: {
            "heroes.$.items.1": "new_value"
        }}
    )
    > db.denis.find()
    {
        "_id" : "43434", 
        "heroes" : [
            {"nickname" : "test", "items" : ["", "new_value", "" ]},
            {"nickname" : "test2", "items" : ["", "", "" ]}
        ]
    }
    

    【讨论】:

    • 如何访问匹配元素的值?比如 {"heros.$.items.2": "heros.$.nickname"} ?
    • @ad7six 如何更新最后一个元素。我不知道数组的大小。 (在 python 中类似于 items[-1] = 'new value')。
    • @Amit 如果您需要:asking that question =)
    【解决方案2】:

    试试update document in array using positional $,

    位置 $ 运算符有助于更新包含嵌入文档的数组。使用位置 $ 运算符访问嵌入文档中的字段,并在 $ 运算符上使用点符号。

    db.collection.update(
      { "heroes.nickname": "test" },
      { $set: { "heroes.$.items.1": "new_value" } },
      { multi: true }
    );
    

    Playground

    【讨论】:

      【解决方案3】:

      此解决方案效果很好。只想加一分。 这是结构。我需要找到 OrderItemId 是 'yyy' 并更新。 如果条件中的查询字段是一个数组,比如下面的“OrderItems.OrderItemId”就是一个数组。您不能在查询中使用“OrderItems.OrderItemId[0]”作为操作。相反,您需要使用“OrderItems.OrderItemId”进行比较。否则,它不能匹配一个。

      {
        _id: 'orderid',
        OrderItems: [
         {
           OrderItemId: ['xxxx'], 
          ... },
         {
           OrderItemId: ['yyyy'], 
          ...}, 
      ]
      
      }
      
       result =  await collection.updateOne(
              { _id: orderId, "OrderItems.OrderItemId": [orderItemId] },
              { $set: { "OrderItems.$.imgUrl": imgUrl[0], "OrderItems.$.category": category } },
              { upsert: false },
            )
          console.log('  (result.modifiedCount) ', result.modifiedCount)
          console.log('  (result.matchedCount) ', result.matchedCount)
      

      【讨论】:

        【解决方案4】:

        尝试使用位置 $ 和 $position 进行更新,

        db.collection.update(
          { heroes:{ $elemMatch:{ "nickname" : "test"} } },
          {
            $push: {
              'heroes.$.items': {
                $each: ["new_value" ],
                $position: 1
              }
            }
          }
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-18
          • 2017-12-16
          • 1970-01-01
          • 2013-09-07
          相关资源
          最近更新 更多