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'

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.