Mongo db driver c# operations with nested array

Hello,

imagine the following scenario:

{
    testId:"abc123",
    name:"object1",
    colors:[
      {
             colorId:1,
             name: Blue,
             description: "This is blue"
      },
      {
             colorId:2,
             name: Green.
             description: "This is green"
      }
    ]
}

I want to be able to perform an upsert based on the following contraints:

  1. if testId does not exist, inserts a new root document.
  2. if testsId exists, check colors. if colorId exists, update existing color, else inserts new element in colors array.

I am able to solve this problem using several separate operations such as:
find the document by testId, perform update and then upsert.

Can this be performed in a single operation using mongodb c# driver? What would be the best way, performance-wise, to do this?

Thanks in advance

I am struggling with a similar problem… Can somebody please help with some ideas? Thanks!