2017-07-10 58 views
0

我对MongoDB非常陌生,但有我认为是非常简单的查询。无法在MongoDB中比较日期

我有一个从IProtein继承蛋白物件(是的,我命名很烂)

public interface IProtein 
{ 
    int Count { get; set; } 
    DateTime Date { get; set; } 
} 

我想返回从集合中FirstOrDefault基于蛋白质对象的Date场的日期比较Today

public IProtein GetProteinForDay(DateTime day) 
    { 
     var collection = _db.GetCollection<IProtein>(DB_COLLECTION); 

     var query = collection.AsQueryable<IProtein>() 
           .Where(p => p.Date == day.Date); 

     var protein = query.FirstOrDefault(); 
     return protein; 
    } 

不幸的是,我已经通过尝试匹配使用MongoDB的日期,所以许多不同的变化了(一些使用LINQ,有些没有),我已经完全多远时I g失去焦点ot与每一个。

这是我当前的代码,则返回错误Unable to determine the serialization information for the expression: p.Date

有什么不对我的查询(是的,它可能是一件很简单的),并实际上,我怎么用的MongoDB/Linq查询比较日期?

+0

我想你可能某处[这里](https://stackoverflow.com/questions/40023067/unable-to找到答案-determine-the-serialization-information-for-expression-using-date) –

+0

@AndrewWinterbotham我在这里通过这个和其他几个答案工作,但他们都没有工作。上面它是什么我问了这个问题 –

回答

2

嗯,令人失望的是.net DateTime无法与MongoDB驱动程序无缝协作。我相信应该支持驱动程序。

无论如何,您需要采取几个步骤才能使.net和MongoDB一起工作。

1)装饰Date字段在您的界面中使用BsonDateTimeOptions属性来告诉MongoDB驱动程序如何序列化.net DateTime。见BsonDateTimeOptions Documentation

界面应该看起来像

public interface IProtein 
{ 
    int Count { get; set; } 

    [BsonDateTimeOptions(Kind = DateTimeKind.Local)] 
    DateTime Date { get; set; } 
} 

2)在你GetProteinForDay功能,更换

var collection = _db.GetCollection<IProtein>(DB_COLLECTION); 
var query = collection.AsQueryable<IProtein>() 
           .Where(p => p.Date == day.Date); 

var collection = db.GetCollection<Protein>(DB_COLLECTION); 
var query = collection.Find(p => p.Date == day); 

注意,我已经取代接口IProtein混凝土实施i接口,在我的例子中是Protein

更新:完整的程序作为参考。

源文件:

{ 
    _id: ObjectID('5964ebf315c46ab80b2c20f3), 
    Count: 10, 
    Date: '2017-07-11 00:00:00.000' 
} 

测试程序:

using System; 
using MongoDB.Bson; 
using MongoDB.Bson.Serialization.Attributes; 
using MongoDB.Driver; 

namespace mongoT 
{ 
    public interface IProtein 
    { 
     ObjectId Id { get; set; } 
     int Count { get; set; } 

     [BsonDateTimeOptions(Kind = DateTimeKind.Local)] 
     DateTime Date { get; set; } 
    } 

    public class Protein : IProtein 
    { 
     public ObjectId Id { get; set; } 
     public int Count { get; set; } 
     public DateTime Date { get; set; } 

     public override string ToString() 
     { 
      return $"{nameof(Id)}: {Id}, {nameof(Count)}: {Count}, {nameof(Date)}: {Date}"; 
     } 
    } 

    class Program 
    { 
     private static string DB = "ProteinsDB"; 
     private static string COLLECTION = "Proteins"; 

     static void Main(string[] args) 
     { 
      var result = GetProteinForDay(DateTime.Now.Date); 
      Console.WriteLine(result); 
     } 

     public static IProtein GetProteinForDay(DateTime day) 
     { 
      var client = new MongoClient(); 
      var db = client.GetDatabase(DB); 

      var collection = db.GetCollection<Protein>(COLLECTION); 
      var query = collection.Find(p => p.Date == day.Date); 

      var protein = query.FirstOrDefault(); 
      return protein; 
     } 
    } 
} 
+0

我觉得我应该知道这一点,但我现在变得“不能转换lambda表达式键入'IMongoQuery',因为它不是委托类型”on“(p => p.Date ==天)“ –

+0

@DarkHippo,我无法重现您报告的错误。请检查你是否有最新的驱动程序。我已经用样本解决方案更新了我的答案。 – Saleem

+0

太好了,谢谢。当我回到我的笔记本电脑时,我会给它一个镜头 –