Work with Array position operator and variables both

Hello how do right syntax for ArrayFilter and var together ?

  var pipeline = new[] 
            BsonDocument.Parse("{ $addFields : { m.$[e].MessageSeqMax: { $sum: ['$m.$[e].MessageSeqMax', 1] }} }"),


                ArrayFilters = new List<ArrayFilterDefinition<BsonValue>>
                    new BsonDocument("e.Id", new BsonDocument("$eq", id))

got error:

Unhandled exception. System.FormatException: Invalid JSON input ''.
   at MongoDB.Bson.IO.JsonScanner.GetNextToken(JsonBuffer buffer)

------- Update 1 for those who dont know C# ------

  "_id" : "8685316d-f26d-4f64-b72b-21fffd95b959", 
  "r" : { "$elemMatch" : { "rid" : "7349d79e-b34a-49f3-8a26-ad420a23878f" } 
} ,
 "update" : [
    {    "$addFields" : { "r.$.MessageSeqMax" : { "$sum" : ["$r.$.MessageSeqMax", 1]  } }    }

Unhandled exception. MongoDB.Driver.MongoCommandException: Command findAndModify failed: Invalid $addFields :: caused by :: FieldPath field names may not start with ‘$’…

Hi, @alexov_inbox,

The JSON string provided to the BsonDocument.Parse method is invalid. Field names in JSON must be quoted, though many parsers allow unquoted field names if they do not contain special characters such as ., [, and ]. The following properly-quoted JSON does parse correctly, but I believe the resulting MQL is invalid:

var pipeline = new[]
            BsonDocument.Parse("{ $addFields : { 'm.$[e].MessageSeqMax': { $sum: ['$m.$[e].MessageSeqMax', 1] }} }"),

Regarding Update 1, this is a problem in your findAndModify command, not in the .NET/C# driver. I would suggest investigating how to express this in the mongosh shell. Providing a self-contained repro (including some simplified sample data) and explanation of what you’re attempting to accomplish would be very helpful in understanding what you are trying to achieve.



Hi @James_Kovacs you always help with C# . Our questing about C# and GUID get top 1 google search result.

Now we current questing move to topic:

If you have time, take a look. there’s a VERY clear and simple example.
We found a complicated answer. But I think there is a simpler , concise answer.