在Python中使用find方法MongoDB之后,如何在数组内添加新字段

时间:2018-08-20 21:39:49

标签: pymongo

伙计们,所以我目前遇到一个问题,我需要在MongoDB中的特定对象的数组中添加一个字段

例如,如果我有此MongoDB对象:

{
    "_id"    : "12345",
    "name"   : "John Doe,
    "videos" : [
         {
            "nested_id" : "1",
            "data1"     : "new lorem",
            "data2"     : "stackoverflow",
            "data3"     : "goldeneye"
         },
         {
            "nested_id" : "2",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond",

         },
         {
            ....
         }     
      ]
}

我想这样做,以便在视频数组中添加一个新字段:

{
    "_id"    : "12345",
    "name"   : "John Doe,
    "videos" : [
         {
            "nested_id" : "1",
            "data1"     : "new lorem",
            "data2"     : "stackoverflow",
            "data3"     : "goldeneye",
            "NEW DATA HERE": "DATA"
         },
         {
            "nested_id" : "2",
            "data1"     : "lorem ipsum",
            "data2"     : "stackoverflow",
            "data3"     : "james bond",
            "NEW DATA HERE": "DATA"
         },
         {
            ....
         }     
      ]
}

现在我只是不确定如何处理它。我目前刚刚使用过更新方法:

 cursor = db.exams.update({"_id":exam['_id']}, {"$set": {"avg_afinn": avg_afinn}})

但是,问题是该字段是为对象而不是视频数组中的内容设置的。

但是我不确定如何解决这个问题。谢谢!

0 个答案:

没有答案