当您不知道文档索引时,在mongodb数组中更新嵌套记录 [英] Updating a nested record in mongodb array when you don't know the document index

查看:55
本文介绍了当您不知道文档索引时,在mongodb数组中更新嵌套记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新嵌套文档的记录,我的文档如下所示:

I want to update the record of a nested document, my document looks like this:

[
  {
    "_id": "60753fd9b249ad0dfa1eeb48",
    "name": "Random Name 1",
    "email": "randomname1@zmel.kom",
    "likings": [
      {
        "breakfast": {
          "eat": "oats",
          "drink": "milk"
        }
      },
      {
        "lunch": {
          "eat": "beef",
          "drink": "pepsi"
        }
      },
      {
        "dinner": {
          "eat": "steak",
          "drink": "champagne"
        }
      }
    ]
  },
  {
    "_id": "60753fd9b249ad0dfa1eeb58",
    "name": "Random Name 2",
    "email": "randomname2@zmel.kom",
    "likings": [
      {
        "breakfast": {
          "eat": "cereals",
          "drink": "coffee"
        }
      },
      {
        "lunch": {
          "eat": "salad",
          "drink": "hot-water"
        }
      },
      {
        "dinner": {
          "eat": "biryani",
          "drink": "apple juice"
        }
      }
    ]
  }
]

现在,我想为 Random Name 2 dinner 修改 drink 的值,但是我不知道晚餐的索引,它可以在午餐之上,也可以在早餐之下.
这是我在Python中尝试过的内容:

Now I want to update the value of drink for dinner for Random Name 2 but I don't know the index of dinner, it could be above the lunch, it could be just below the breakfast.
Here's what I have tried in Python :

oid = data[0]                # fetched from flask form
to_be_updated = data[1]      # fetched from flask form
update_value = data[2]       # fetched from flask form
condition = {"_id" : oid}
update_value = {
    "$set" : {
        f"likings.{to_be_updated}.drink" : update_value
    }
}
response = mongo.db.food.update(condition, update_value)

但我遇到的错误是:
pymongo.errors.WriteError:无法在元素<完整元素描述>
中创建字段晚餐"我计划使用的其他策略是,只匹配ID,然后通过保留不需要更改的值并更改我要更改的值来更新 likings .但是这种方法似乎太明显了,从语义上来说是错误的,因为我使用的不是更新而是更新这种策略,即没有任何实际原因干扰收集模式.有没有办法做到这一点,还是我应该继续我的想法

but the error I am getting is:
pymongo.errors.WriteError: Cannot create field 'dinner' in element <complete element description>
What my other strategy that I am planning on using is, only match ID and then update likings by keeping the values that are no needed to be changed as it is and altering that value that i want to change. But this approach seems too obvious and semantically wrong since I am using not updating but updating kind of strategy i.e disturbing the collection schema for no actual reason. Is there a way to do that or should I just continue with what I am thinking

推荐答案

演示:MongoDB游乐场

首先,您的JSON中有错误.

First of all, there are errors in your JSON.

[
  {
    "_id": "60753fd9b249ad0dfa1eeb48",
    "name": "Random Name 1",
    "email": "randomname1@zmel.kom",
    "likings": [
      {
        "breakfast": {
          "eat": "oats",
          "drink": "milk"
        }
      },
      {
        "lunch": {
          "eat": "beef",
          "drink": "pepsi"
        }
      },
      {
        "dinner": {
          "eat": "steak",
          "drink": "champagne"
        }
      }
    ]
  },
  {
    "_id": "60753fd9b249ad0dfa1eeb58",
    "name": "Random Name 2",
    "email": "randomname2@zmel.kom",
    "likings": [
      {
        "breakfast": {
          "eat": "cereals",
          "drink": "coffee"
        }
      },
      {
        "lunch": {
          "eat": "salad",
          "drink": "hot-water"
        }
      },
      {
        "dinner": {
          "eat": "biryani",
          "drink": "apple juice"
        }
      }
    ]
  }
]

尝试一下:

db.collection.update({
  "name": "Random Name 2",
  "likings.dinner": {
    "$exists": true
  }
},
{
  "$set": {
    "likings.$.dinner.drink": "PEPSI"
  }
})

您可以将晚餐更改为要相应更新的任何字段.

You can change dinner to whatever field you want to update accordingly.

这篇关于当您不知道文档索引时,在mongodb数组中更新嵌套记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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