I am developing a .NET application with MongoDB, using C# driver 2.14. My query is as follows:
I have an embedded array elements, eg:
{
"_id" : 1,
"Friends" : [
{
"_id" : 2,
"FirstName" : "Bob",
"LastName":"Marley",
"Gender":"Male",
...
},
{
"_id" : 3,
"Name" : "Jonson",
"LastName":"Charles",
"Gender":"Male",
...
},
{
"_id" : 4,
"Name" : "Bob",
"LastName":"",
"Gender":"Male",
...
}
]
}
which has multiple fields in it. I need to update all the values in one of the nested documents.
I have written a code as follows:
var filter1 = Builders<BsonDocument>.Filter.Eq("_id", 1);
var filter2 = Builders<BsonDocument>.Filter.ElemMatch<BsonValue>("Friends", new BsonDocument(){ {"_id", 2 }});
var update = Builders<BsonDocument>.Update.Set("Friends.$[-1]", BsonDocument.Parse(JsonConvert.SerializeObject(object)));
await collection.UpdateAsync(filter1&filter2, update);
I am getting the exception as:
No array filter found for identifier '-1'
Can anyone please suggest whether it's the right approach?
CodePudding user response:
It is an invalid syntax/statement in MongoDB for
"Friends.$[-1]"


