简体   繁体   中英

MongoDB replace array inside array w/ C# driver

I need to replace the contents of 'baz.'

{
  "_id" : ObjectId("6058f722e9e41a3d243258dc"),
  "fooName" : "foo1",
  "fooCode" : 1,
  "bar" : [
    {
      "barCode" : "123",
      "barName" : "Rick's Cafe",
      "baz" : [
        {
          "bazId" : "00",
          "bazDescription" : "Ilsa"
        },
        {
          "bazId" : "21",
          "bazDescription" : "Victor"
        }
      ]
    }
  ]
}

I started with UpdateOneAsync and the filter

Expression<Func<Foo, bool>> filter =
  f => f.fooCode == 1 &&
  f.Bar.Any(b => s.BarCode == "123")

And once I started typing the update statement

Builders<Foo>.Update.Set(f => ??? , newBazArray);

I realized I was probably doing this incorrectly. How should I replace the array baz?

You should be able to use this:

f => f.Bar[-1].baz

Where [-1] is equivalent to the positional operator ($) in a Mongo query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM