I have a MongoDB collection with a document structure similar to:
{
"_id" : "xxx",
"inner : {
"foo" : 1,
"bar" : 2
}
}
Database version is 4.2.6
and an aggregation pipeline whose penultimate stage is $project with an outcome document matching something like this one:
{
"_id" : "xxx",
"inner : {
"baz" : 3
}
}
Followed by a final stage to merge into the collection:
{
"$merge" : {
into: "myCollection",
on: "_id",
whenMatched: "merge",
whenNotMatched: "discard"
}
}
However as a result, the existing inner document fields are overridden by the $merge results. Meaning:
Expected results:
{
"_id" : "xxx",
"inner : {
"foo" : 1,
"bar" : 2,
"baz" : 3
}
}
Actual results:
{
"_id" : "xxx",
"inner : {
"baz" : 3
}
}
How can I overcome this?