How to handle case: create or update nested object in Mongo within one upsert

Hello, I’m struggling long time with case to create or update nested object in below structure:


A want to create new object pfmCtgy in list pfmCtgyLst only if in list doesn’t exist object with pfmCtgy.pfmSrc=‘A’. If object with pfmSrc exists I want to update its field. I have below code, but doesn’t work:

Update update = new Update()
        .addToSet("finOprtn.pfmCtgyLst.$[pfmCtgy]", new PfmCty())

I handled update object in structure, but I can not achieve create object if not exist in array.
Thanks in advance for any help.

Hello @12345alcatraz_N_A ,

Welcome to The MongoDB Community Forums! :wave:

To understand your use-case better, can you please share more details such as:

  • MongoDB version
  • Sample documents
  • Expected output
  • Brief description of your requirements with regards to the sample documents and expected output.
  • Programming language used
  • Driver version