如何处理c#中的{Document} is not supported.
异常?
想把下面的MongoDB查询转换成c#代码:
db.Data.find({
"searchData": "Abc",
"modifyDates": {
$elemMatch:{
$gte: ISODate("2022-07-01T07:00:00Z"),
$lt: ISODate("2022-07-04T07:00:00Z")
}
}
})
c#中的模型:
public class MyData
{
public ObjectId Id { get; set; }
public string searchData { get; set; };
public List<DateTime> modifyDates
{
get => _modifyDates ?? new List<DateTime> { lastChange };
set => _modifyDates = value?.OrderBy(d => d).ToList();
}
private List<DateTime> _modifyDates;
}
MongoDB c# builder(两种情况下都不工作):
// start and end dates for filtering records
var start = = DateTime.Parse("01/06/2022");
var end = = DateTime.Parse("10/06/2022");
// case1:
var builder = Builders<MyData>.Filter;
var filter = builder.ElemMatch(f => f.modifyDates, d => d >= start && d <= end);
// case2:
var builder = Builders<MyData>.Filter;
var filter = builder.ElemMatch(f => f.modifyDates, Builders<DateTime>.Filter
.Where(d => d >= start && d <= end));
// no matter from case1/2, there is exception `{Document} is not supported.`
return await Collection.Find(filter);
请帮忙,谢谢。
错误提示您不能在此特定情况下使用表达式收入参数d
。我认为这种情况还没有在驱动程序中实现。您可以使用原始的bson文档表单,而不是键入:
var builder = Builders<MyData>.Filter;
var filter = builder.ElemMatch(f => f.modifyDates, new BsonDocument { { "$gte", start }, { "$lte", end } });