I'm trying to read from a mongoDB collection with a Date filter (only the Date, time should be ignored):
var filterDefinition = Builders<SomeClass>.Filter.Eq(p => p.SomeDateTimeProperty.Date, DateTime.Now.Date);
using (var cursor = await _someCollection.FindAsync(filterDefinition))
{ ... }
SomeClass
and the SomeDateTimeProperty
property look like this:
[BsonIgnoreExtraElements]
[Serializable]
public class ReportConfiguration
{
...
public DateTime SomeDateTimeProperty { get; set; }
...
}
The code throws an InvalidOperationException
when trying to .FindAsync()
:
An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code Additional information: Unable to determine the serialization information for p => p.SomeDateTimeProperty.Date
Everything works if I remove the .Date part in the filter (p.SomeDateTimeProperty.Date, DateTime.Now.Date), but I need a yyyy\mm\dd comparison regardless of the hh\mm.
Stacktrace says:
at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.SimpleFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindAsync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.IMongoCollectionExtensions.FindAsync[TDocument](IMongoCollection`1 collection, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at [...]
What could be the issue?