MongoDB .NET 驱动程序 - 将字符串转换为日期时间并用于过滤器生成器

发布于 2025-01-13 11:53:50 字数 650 浏览 4 评论 0原文

var builder = Builders<ModelClass>.Filter;
var filter = builder.Where(x => x.Active);

if (fromDate.HasValue)
{
    var date = fromDate.Value;
    var subfilter = builder.Where(x => DateTime.Parse(x.EnrollmentDate) >= date);
    filter &= subfilter;
}

注册日期保存为字符串:

public string EnrollmentDate { get; set; }

,我需要过滤一组日期范围内的文档,但如何比较?我需要这样过滤。

我明白了

System.InvalidOperationException:不支持 Parse({document}{EnrollmentDate})。

subfilter 行出错。

var builder = Builders<ModelClass>.Filter;
var filter = builder.Where(x => x.Active);

if (fromDate.HasValue)
{
    var date = fromDate.Value;
    var subfilter = builder.Where(x => DateTime.Parse(x.EnrollmentDate) >= date);
    filter &= subfilter;
}

Enrollment Date is saved as a string:

public string EnrollmentDate { get; set; }

, I need to filter docs within a set of date range, but how do I compare this? I need to filter like this.

I get

System.InvalidOperationException: Parse({document}{EnrollmentDate}) is not supported.

Error in subfilter line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剑心龙吟 2025-01-20 11:53:50

我认为您需要使用 MongoDB 查询来实现,如下所示:

{
  "$expr": {
    "$gte": [
      { "$toDate": "$EnrollmentDate" },
      date
    ]
  }
}

虽然我认为使用 MongoDB .Net 驱动程序 LINQ 语法无法实现,但您可以将查询转换为 BsonDocument

var subfilter = new BsonDocument("$expr",
    new BsonDocument("$gte", 
        new BsonArray {
            new BsonDocument("$toDate", "$EnrollmentDate"),
            date
        }
    )
);

filter &= subfilter;

I think you need to achieve with MongoDB query as below:

{
  "$expr": {
    "$gte": [
      { "$toDate": "$EnrollmentDate" },
      date
    ]
  }
}

While I think it is not achievable with MongoDB .Net Driver LINQ syntax, you convert the query as BsonDocument:

var subfilter = new BsonDocument("$expr",
    new BsonDocument("$gte", 
        new BsonArray {
            new BsonDocument("$toDate", "$EnrollmentDate"),
            date
        }
    )
);

filter &= subfilter;
彼岸花ソ最美的依靠 2025-01-20 11:53:50

当您想要执行 DateTime.Parse() 时,您会遇到问题。

您可以发布字符串 EnrollmentDate 的格式吗?你的变量 date ,只是 Date 还是 DateTime ?

这个也许可以帮助你这里

另外,尝试使用

var subfilter = builder.Gte(x=>x.Y, Z) 

You have problem here when you want to do DateTime.Parse()

Can you post format of your string EnrollmentDate? And your variable date , is it only Date or DateTime?

This one maybe can help you here

Also, try to use

var subfilter = builder.Gte(x=>x.Y, Z) 
深居我梦 2025-01-20 11:53:50

我在 MongoDB 中的 DateTime 解析时遇到了同样的错误,因此我建议您将模型中的 EnrollmentDate 重构为 DateTime。 MongoDB 知道如何保留 DateTime 以及如何根据它进行过滤和排序,而无需更改子过滤器。

I had the same error with DateTime parsing in MongoDB so I'd suggest you to refactor EnrollmentDate in your model to DateTime. MongoDB knows how to persist DateTime and how to Filter and Sort by it without having to change your subfilters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文