2017-07-10 57 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

太好了,謝謝。當我回到我的筆記本電腦時,我會給它一個鏡頭 –