在 MongoDB 集合中文档的列表字段中的子文档中更新 Pymongo 布尔字段

Update with Pymongo boolean field in a subdocument within a list field of a document in a MongoDB collection

示例数据库

下面是示例数据库的集合:

{
  {
    "_id": "fijo_1"
    "Procesos": [{
        "code_a": "1234",
        "code_b": "5678",
        "code_c": "9012",
        "vector_aux": ["01", "02"],
        "leido": false
    }, {
        "code_a": "0000",
        "code_b": "1111",
        "code_c": "2222",
        "vector_aux": ["03", "04"],
        "leido": false
    }  
    }]
  },
  {
    "_id": "fijo_2"
    "Procesos": [{
        "code_a": "3333",
        "code_b": "4444",
        "code_c": "5555",
        "vector_aux": ["01", "02"],
        "leido": false
    }, {
        "code_a": "6666",
        "code_b": "7777",
        "code_c": "8888",
        "vector_aux": ["03", "04"],
        "leido": false
    }  
    }]
  }
}

问题

我需要更新或更改文档的特定字段的值,该文档位于一个数组位置,该数组位置本身就是 dbdd 文档的一个字段。 更具体地说,我需要将字段 "leido" 更改为 True程序编号2“程序” 对于 "_id " 命名为 fijo_2,通过完成一个过滤器,对应匹配 "code_a" 与 "3333","code_b" 与 "4444" 和 "code_c" 与“5555”,是满足多个条件的过滤器。

编程语言和库:

我使用 Python 3.9 和“pymongo”库以及在 MongoDB 中创建的基础。

问题由 YuTing

解决

这是我现在可以使用的代码:

filter_aux = {'_id': "ES0031104316853001ED_1389"}
updtae_aux = {'$set': {"Procesos.$[element].leido": True}}
array_filters_aux= [{'element.codeA': "C1", 'element.codeB': "01", 'element.codeC': "000000000051", 'element.leido': False}]
collection.update_one(filter=filter_aux, update=updtae_aux,upsert=True, array_filters= array_filters_aux)

使用$arrayFilters

db.collection.update({
  "_id": "fijo_2"
},
{
  $set: {
    "Procesos.$[element].leido": true
  }
},
{
  arrayFilters: [
    {
      "element.code_a": "3333",
      "element.code_b": "4444",
      "element.code_c": "5555",
      
    }
  ],
  upsert: true
})

mongoplayground