C# 按日期字段查找mongodb中的文档+;一些日期值

C# 按日期字段查找mongodb中的文档+;一些日期值,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我需要一些C#Mongo驱动程序和mongodb的帮助 C类示例: public class Document { public string Id { get; set; } public DateTime StartTime { get; set; } public TimeSpan CustomPeriod { get; set; } } 我需要一种通过StartTime字段+一些TimeSpan值在mongodb中查找文档的方法,如以下谓词: Expr

我需要一些C#Mongo驱动程序和mongodb的帮助

C类示例:

 public class Document
 {
    public string Id { get; set; }

    public DateTime StartTime { get; set; }

    public TimeSpan CustomPeriod { get; set; }
 }
我需要一种通过StartTime字段+一些TimeSpan值在mongodb中查找文档的方法,如以下谓词:

Expression<Func<Document, bool>> customExpression = x 
    => x.StartTime.Add(x.CustomPeriod) <= DateTime.UtcNow;
Expression customExpression=x

=>x.StartTime.Add(x.CustomPeriod)问题在于MongoDB实例中的底层命令使用的查询语言不支持将文档中的一个字段与另一个字段进行比较,或者在比较之前对这些字段执行操作

运算符允许使用聚合表达式,但您通常无法为查询的该部分使用索引

在mongo shell中,它可能看起来像:

db.collection.find({
      $expr:{
         $gte:[ 
            new Date(), 
            {$add: [ "$StartTime", "$CustomPeriod"]}
         ]
      }
})

我不熟悉C#,因此我不知道如何使用.net驱动程序来表达这一点,虽然不完全相同,但这里的答案可能会有所帮助