Below is the collection of an example database:
{
{
"_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
}
}]
}
}
I need to update or change the value of a specific field of a document that is hosted in an array position that is itself a field of a dbdd document. More specifically, I need to change the field "leido" to True of the proceso number 2 of "Procesos " for the "_id " named fijo_2, by fulfilling a filter, corresponding to match "code_a" with "3333", "code_b" with "4444" and "code_c" with "5555", being a filter that fulfils several conditions.
I use Python 3.9 and the "pymongo" library as well as the base created in MongoDB.
Here is my code that now works:
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)
use $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
})