1

I need to update point Description field highlighted inside the nested Document using C# Mongo DB driver. I can do this update successfully using following query.

Document Structure


    {
      "ControllerID": "testcon",
      "CCNID": "testccn",
      "TableGroup": 1,
      "Tables": [
        {
          "ID": 0,
          "TableGroupID": 1,
          "Blocks": [
            {
              "ID": 0,
              "TableID": 0,
              "TableGroupID": 1,
              "ControllerID": "testcon",
              "Points": [
                
                {
                  "BlockID": 0,
                  "TableGroupID": 1,
                  "ControllerID": "testcon",
                  "TableID": 0,
                  "PointDefinitionID": 23,
                  "Name": "Test Point",
                  "Description": "Hgfhdhfhey You"   <----------- This field needs to be updated
                },
               {
                  "BlockID": 0,
                  "TableGroupID": 1,
                  "ControllerID": "testcon",
                  "TableID": 0,
                  "PointDefinitionID": 24,
                  "Name": "Test Point",
                  "Description": "Hgfhdhfhey You"
                }
              ]
            }
          ]
        }
      ]
    }

I can successfully update the point Description using this query.


    db.ControllerPointCollection.updateOne({
      "_id": "HRDC_testccn_0_34_1"
    },
    {
      $set: {
        "Tables.$[t].Blocks.$[b].Points.$[p].Description": "Hey You"
      }
    },
    {
      arrayFilters: [
        {
          "t.ID": 0
        },
        {
          "b.ID": 0
        },
        {
          "p.PointDefinitionID": 23
        }
      ]
    })

I tried using this filter and update object for the above operation


  var pointFilter = Builders<Point>.Filter.Eq(p => p.PointDefinitionID, point.PointDefinitionID);

  var blockFilter = Builders<Block>.Filter.Eq(b => b.ID, point.BlockID) & Builders<Block>.Filter.ElemMatch(b => b.Points, pointFilter);

  var tableFilter = Builders<Table>.Filter.Eq(t => t.ID, point.TableID) & Builders<Table>.Filter.ElemMatch(t => t.Blocks, blockFilter);

  var filter = Builders<ControllerPointDataDoc>.Filter.Eq(c => c.ID, point.ControllerID) & Builders<ControllerPointDataDoc>.Filter.ElemMatch(c => c.Tables, tableFilter);

  var updater = Builders<ControllerPointDataDoc>.Update.Set(c => c.Tables[-1].Blocks[-1].Points[-1].Description, "Hey You");

  operationResult.Data = await ControllerPointDataCollection.UpdateOneAsync(filter, updater);

but I am getting the following error.

A write operation resulted in an error.\r\n Too many positional (i.e. '$') elements found in path 'Tables.$.Blocks.$.Points'

CleanCoder540
  • 57
  • 1
  • 8
  • Does this answer your question? [C# - MongoDB - Update an element inside a Nested Document](https://stackoverflow.com/questions/48180188/c-sharp-mongodb-update-an-element-inside-a-nested-document) – Joe Apr 10 '21 at 08:11
  • Yes , this answers my question. – CleanCoder540 Apr 10 '21 at 10:17

0 Answers0