I'm trying to upsert a specific array element in a MongoDB document.
this is an example of a document from my collection:
{
"_id":3,
"name":alex,
"array": [
{
"_id":1,
"a":30,
"b":60
},
{
"_id":2,
"a":40,
"b":80
}
]
}
- In case where "array._id" exist I want to update its "a" value without changing "b" value. Output example:
{
"_id":3,
"name":alex,
"array": [
{
"_id":1,
"a":90,
"b":60
},
{
"_id":2,
"a":40,
"b":80
}
]
}
- In case where "array._id" doesn't exist I want to create such entry with the given "_id" and "a" values in "array". Output example:
{
"_id":3,
"name":alex,
"array": [
{
"_id":1,
"a":30,
"b":60
},
{
"_id":2,
"a":40,
"b":80
},
{
"_id":4,
"a":50,
}
]
}
- In case where "array" doesn't exist I want to create it and insert to it an entry with the given "_id" and "a" values. Output example:
{
"_id":3,
"name":alex,
"array": [
{
"_id":1,
"a":30,
}
]
}