After upgrade the driver version from 2.18.0
to 2.24.0
the index based “LINQ Expression” used to update a specific item in array do not works anymore:
Used expression:
Builders<Traveler>.Update.Set(t => t.VisitedCountries[-1].Name, "Hellas")
Full example:
var travelersCollection = database
.GetCollection<Traveler>(Constants.TravelersCollection);
// create an elemMatch operator
var visitedGreeceExactly3Times = Builders<Traveler>.Filter
.ElemMatch(t => t.VisitedCountries,
country => country.Name == "Greece"
&& country.TimesVisited == 3);
// create the update definition
var updateDefinition = Builders<Traveler>.Update.Set(t => t.VisitedCountries[-1].Name, "Hellas");
// this will update only the first matching array element!
// ($) refers to the first match
var updateHellasResult = await travelersCollection
.UpdateManyAsync(visitedGreeceExactly3Times,
updateDefinition);
After research, the current suggestion is:
Builders<Traveler>.Update.Set("visitedCountries.$.Name", "Hellas");
But in this case, we need to put “some magic MQL string expression” into code, is there another way to do this with full “LINQ Expression”, like before?
Builders<Traveler>.Update.Set(t => t.VisitedCountries.<something-new>, "Hellas");
It’s a breaking change on LinqProvider.V3
used by this driver version?
UPDATE:
There are a related ISSUE CSHARP-4079, but apparently its closed marked as solved due to lack of documentation of the differences between LINQ2 and LINQ3 providers. A new “sub issue” has been created about it: CSHARP-4618.
Any suggestions or considerations?
Thanks!