[CSHARP-2292] FilterDefinitionBuilder error {document}.FieldName is not supported Created: 05/Jun/18  Updated: 27/Oct/23  Resolved: 15/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: AMOUZIGH Driss Assignee: James Kovacs
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-3615

 Description   

Hello,

I'm using trying to do an upset using this code:

List<UpdateOneModel<IChannelItem>> requests = new List<UpdateOneModel<IChannelItem>>(items.Count);
foreach (IChannelItem entity in items)

{ string sId = entity.Identifier; var filter = new FilterDefinitionBuilder<IChannelItem>().Where(m => m.Identifier.Equals(sId)); var update = new UpdateDefinitionBuilder<IChannelItem>() .Set(m => m.Value , entity.Value) .Set(m => m.Timestamp_utc, entity.Timestamp_utc) .Set(m => m.Timestamp_local, entity.Timestamp_local) .Set(m => m.DateAction, entity.DateAction); var request = new UpdateOneModel<IChannelItem>(filter, update); request.IsUpsert = true; requests.Add(request); }

 

Classe IChannelItem:

public interface IChannelItem : IDataItem

{ int Id \{ get; set; }

string Identifier

{ get; }

string Type
{ get; }

string Value

{ get; set; }

string Quality
{ get; set; }

string Timestamp_utc

{ get; set; }

string Timestamp_local
{ get; }

DateTime DateAction
{ get; }

string Origin
{ get; }
bool added
{ get; set; }

string JSon

{ get; }

}

 

I'm getting this error:

{document}.Identifier is not supported.
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateEquals(MethodCallExpression methodCallExpression)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateMethodCall(MethodCallExpression methodCallExpression)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate[TDocument](Expression`1 predicate, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry)
 at MongoDB.Driver.ExpressionFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
 at MongoDB.Driver.MongoCollectionImpl`1.ConvertWriteModelToWriteRequest(WriteModel`1 model, Int32 index)
 at System.Linq.Enumerable.<SelectIterator>d__5`2.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
 at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation..ctor(CollectionNamespace collectionNamespace, IEnumerable`1 requests, MessageEncoderSettings messageEncoderSettings)
 at MongoDB.Driver.MongoCollectionImpl`1.CreateBulkWriteOperation(IEnumerable`1 requests, BulkWriteOptions options)
 at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
 at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass23_0.<BulkWrite>b__0(IClientSessionHandle session)
 at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
 at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)

 

Regards,

Driss



 Comments   
Comment by James Kovacs [ 15/Feb/22 ]

The driver's serialization code requires setters to modify properties. This includes deserializing instances of a class. Consider the following property definition:

public int Value { get; }

The compiler generates an auto-prop getter that returns the value of the underlying compiler-generated field. There is no compiler-generated setter for setting that field. The driver would have to guess the name of the backing field based on compiler naming conventions. We choose not to do that and instead rely on compiler-generated setters. These setters can be marked as private or init if you do not want users of your class to modify them.

public int Value1 { get; private set; } // only modifiable by the class itself
public int Value2 { get; init; } // only modifiable in the constructors of the class

If you modify your class definition to include private set or init for those properties, the class can be serialized and deserialized successfully.

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