[CSHARP-4079] Implement positional update operators in LINQ3 Created: 24/Feb/22  Updated: 15/Dec/23  Resolved: 24/Mar/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.14.0
Fix Version/s: 2.16.0

Type: Bug Priority: Major - P3
Reporter: James Kovacs Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-2232 Add Support for $[] array operator Closed
Related
is related to CSHARP-4618 Cannot find documentation on differen... Scheduled
Epic Link: CSHARP-3615

 Description   

In LINQ2, you could use the magic number -1 to indicate use of the positional operator, but this no longer works in LINQ3. When calling UpdateOneAsync, UpdateManyAsync, and related operations, the following code works in LINQ2, but fails in LINQ3:

await collection.UpdateOneAsync(l => l.Id == another.Id && l.AnArrayMember.Any(l => l.Id == anArrayId),
                    Builders<ModelClass>.Update.Set(l => l.AnArrayMember.ElementAt(-1).Deleted, true));

In LINQ3 this fails with the error "Cannot create field '-1' in element [...]". In LINQ2 it successfully translated it into "AnArrayMember.$.Deleted" and the Set command completed fine.



 Comments   
Comment by Paul Bowden [ 15/Dec/23 ]

An issue that I've come across with this change is that I must now add a reference to the MongoDB library when I didn't need to before as my data layer was abstracted in my business logic. I can't think of a workaround to this, so have had to add the reference which is a bit annoying.

Comment by Githook User [ 24/Mar/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4079: Implement positional update operators in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/0be98c5addaba2e160fbe3a290dee75d022588ef

Comment by Robert Stam [ 21/Mar/22 ]

-1 was not a good choice as way to represent the positional operator.

In LINQ3 we are going to be using custom extension methods instead to represent the three different kinds of positional operators available in update specifications:

x.A.FirstMatchingElement() => "A.$"
x.A.AllElements() => "A.$[]"
x.A.AllMatchingElements("identifier") => "A.$[identifier]"

Generated at Wed Feb 07 21:47:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.