When using $mergeObject in update pipeline, the original object is being
overwritten. Are there any alternatives to avoid this from happening or can $mergeObjects be used in any way to avoid this.
Yes this will work. But I have a large document and setting like this was not feasible. That’s why I am trying to use update pipeline. And the update will not always have only one key-value.
This back and forth discussion with correct solutions provided by @santimir is due to the fact that you do not publish real and complete samples of input documents and desired resulting documents.
You keep bringing new facts which make it difficult to provide a complete working solutions.
This looks like your other thread that ended up abruptly from your lack of response.
This somewhat works for me right now as I am injecting the second part of first $mergeObjects as per data needs to make a sort of prepared query. As I was needing the top level $set to begin from employment (Sorry about unclear question) I can probably make go with the shown method! Thanks for the interest!
Nested fields like selfEmployed occur less. So I can use the query I originally mentioned and based on some conditions if I need to do nested merge, I can inject some separate logic as second $mergeObjects parameter.
But If i go the way as mentioned by @santiago_miranda1 I will need to write a new query. If I go the way I mentioned I can just extend the already existing query(which might be and some are part of larger queries).
However I will agree that what I answered is not the best solution! I’ll be looking for better soultions!