[CSHARP-4577] LINQ3 provider throws ExpressionNotSupportedException when using Unset on a dictionary key Created: 22/Mar/23  Updated: 29/Mar/23  Resolved: 27/Mar/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.18.0, 2.19.0
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Mox Poe Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: LINQ, LINQ3
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4562 SortBy failure for dictionary entry i... Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

LINQ3 provider throws below exception when using `Unset` on a dictionary key :

Unhandled exception. MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression must be a constant: value(Program+<>c__DisplayClass0_0).key in o.Keys.get_Item(value(Program+<>c__DisplayClass0_0).key).

MongoDB server version: 5.0.2 (standalone)
MongoDB driver version: 2.19.0

How to Reproduce

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
// Init.
var mongoUrl = new MongoUrl("mongodb://localhost");
var settings = MongoClientSettings.FromUrl(mongoUrl);
settings.LinqProvider = LinqProvider.V3; // WORKS WITH V2.
var client = new MongoClient(settings);
var database = client.GetDatabase("default");
var collection = database.GetCollection<Model>("Models");
 
// Insert document with key.
var id = ObjectId.GenerateNewId();
var key = "key";
var model = new Model(id, new Dictionary<string, string> { [key] = "value" });
collection.InsertOne(model);
 
// ✅ Update value.
collection.UpdateOne(
    Builders<Model>.Filter.Eq(o => o.Id, id),
    new UpdateDefinitionBuilder<Model>().Set(o => o.Keys[key], "new value"));
 
// ❌ Remove key.
collection.UpdateOne(
    Builders<Model>.Filter.Eq(o => o.Id, id),
    new UpdateDefinitionBuilder<Model>().Unset(o => o.Keys[key]));
 
 
record Model(ObjectId Id, Dictionary<string, string> Keys);



 Comments   
Comment by Mox Poe [ 29/Mar/23 ]

It works for me as well in version 2.19.1, thank you.

Comment by Robert Stam [ 27/Mar/23 ]

I have verified that this has the same underlying issue as CSHARP-4562 and that it has been fixed in version 2.19.1.

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