Hello, I have a question regarding atomic upsert into mongo DB which includes merging arrays.
For instance I have the following document in Mongo DB:
{
date: 2020-12-12,
type: 4,
payload: {
type: some_type,
ids: [1], - one element always, finding documents by this field
prices: [
{
id: 111,
value: 20
},
{
id: 222,
value: 30
}
]
}
}
and I received the document
{
date: 2021-01-01,
type: 4,
payload: {
type: some_type,
ids: [1], - one element always, finding documents by this field
prices: [
{
id: 333,
value: 20
},
{
id: 222,
value: 20
}
]
}
}
I need to check in DB if there is a document with the same payload.id. If no then insert the received one. If yes then merge prices (if price with price.id exists then update it, if not - insert it). In the result I need to have the following document in DB:
{
action: POST,
version: 4,
payload: {
type: some_type,
ids: [1], - one element always, finding documents by this field
prices: [
{
id: 111,
value: 20
},
{
id: 333,
value: 20
},
{
id: 222,
value: 20
}
]
}
}
Is it possible do atomically via Mongo query and if it possible to do via Java Aggregation framework?