I'm pretty new in mongodb so here's my question, what I want is check if the main document exists and then if it does, check if the nested document exist then update it and if it doesn't insert a new one. But with this code I can only update an existent nested document but I cannot create a new one. I don't even know if it is a proper use of this method, is it efficient?
collection.find_one_and_update(
{"_id:{"field1":val1,"field2":val2,"field3":val3,"field4":val4}},
{"$set":{"elements.$[element].prop2":valToUpdate}},
return_document=ReturnDocument.AFTER,
array_filters=[{"element.prop1":valToCheck}],
upsert=True
)
In mongo syntax I think is something like this:
db.collection.findOneAndUpdate(
{"_id:{"field1":val1,"field2":val2,"field3":val3,"field4":val4}},
{"$set":{"elements.$[element].prop2":valToUpdate}},
{ arrayFilters: [ {"element.prop1":valToCheck} ] }
)
Can I use a single function like this one for do the whole process (check if the main and the nested document exist and if it does update, otherwise create a new one)?
A sample of how I want to alter the document:
before execute the method
{
"_id":{"name":"Storename","location":"somewhere","phone":46284723},
"books":
[
{"name":"somebook","price":{"$numberDouble":"34"}}
]
}
After the execution if there's the nested document
{
"_id":{"name":"Storename","location":"somewhere","phone":46284723},
"books":
[
{"name":"somebook","price":{"$numberDouble":"55"}},
]
}
After the execution if there's not the nested document
{
"_id":{"name":"Storename","location":"somewhere","phone":46284723},
"books":
[
{"name":"somebook","price":{"$numberDouble":"34"}},
{"name":"someOTHERbook","price":{"$numberDouble":"45"}}
]
}