MongoDb c#驱动程序在DateTime比较中抛出序列化错误

本文关键字:序列化 错误 比较 驱动程序 DateTime MongoDb | 更新日期: 2023-09-27 18:07:45

我正在使用MongoDb的c#驱动程序。我有一个非常简单的'查找'查询:

var cursor = PortalContext.Users.Find(user => DateTime.Now > user.UpdatedDate);
return cursor.ToListAsync();

抛出异常:

Message: "An error has occurred."
ExceptionMessage: "Unsupported filter: (8/13/2015 12:03:44 PM > Serialization(UpdatedDate))."
ExceptionType: "System.ArgumentException"
StackTrace: " at MongoDB.Driver.Linq.Translators.PredicateTranslator.BuildFilter(Expression expression) at ...

MongoDb c#驱动程序在DateTime比较中抛出序列化错误

尝试重新安排您的Find,使常数(DateTime.Now)在右手边,这工作吗?

var cursor = PortalContext.Users.Find(user => user.UpdatedDate < DateTime.Now);
return cursor.ToListAsync();

有一个错误在1。x驱动程序(CSHARP-431),其中驱动程序没有处理常量在左侧的这种情况。这个问题在很久以前就修复了,但这个问题可能是那个问题的回归。