2013-04-05 58 views
0

我需要針對匹配多個屬性的文檔集合進行查詢。
(從郵件列表交叉後:https://groups.google.com/forum/?fromgroups=#!topic/ravendb/r5f1zr2jd_o使用多個屬性查詢RavenDB文檔

下面是文檔:

public class SessionToken 
{ 
    [JsonProperty("jti")] 
    public string Id { get; set; } 

    [JsonProperty("aud")] 
    public Uri Audience { get; set; } 

    [JsonProperty("sub")] 
    public string Subject { get; set; } 

    [JsonProperty("claims")] 
    public Dictionary<string, string> Claims { get; set; } 
} 

這裏是測試:

[TestFixture] 
public class RavenDbTests 
{ 
    private IDocumentStore documentStore; 

    [SetUp] 
    public void SetUp() 
    { 
     this.documentStore = new EmbeddableDocumentStore() { RunInMemory = true }; 
     this.documentStore.Initialize(); 
    } 

    [Test] 
    public async void FirstOrDefault_WhenSessionTokenExists_ShouldReturnSessionToken() 
    { 
     var c = new SessionToken() 
       { 
        Audience = new Uri("http://localhost"), 
        Subject = "NUnit", 
        Claims = new Dictionary<string, string>() 
           { 
            { ClaimTypes.System, "NUnit" } 
           } 
       }; 

     using (var session = this.documentStore.OpenAsyncSession()) 
     { 
      await session.StoreAsync(c); 
      await session.SaveChangesAsync(); 

      // Check if the token exists in the database without using Where clause 
      var allTokens = await session.Query<SessionToken>().ToListAsync(); 
      Assert.That(allTokens.Any(x => x.Subject == "NUnit" && x.Audience == new Uri("http://localhost"))); 

      // Try getting token back with Where clause 
      var token = await session.Query<SessionToken>().Customize(x => x.WaitForNonStaleResults()).Where(x => x.Subject == "NUnit" && x.Audience == new Uri("http://localhost")).ToListAsync(); 
      Assert.IsNotNullOrEmpty(token.First().Id); 
     } 
    } 
} 

最後斷言是失敗的一個。 我必須承認我不確定這是我的錯誤還是失敗。
據我瞭解,這應該是工作。

PS。我曾嘗試過使用獨立文檔存儲,並且沒有在內存中運行,但結果相同。

回答

2

您正在收到陳舊的結果。在單元測試中,您需要留出時間進行索引編制。

.Customize(x=> x.WaitForNonStaleResults())添加到您的查詢,測試應通過。

此外,我認爲當您剪切/粘貼時,因爲它不按原樣編譯,所以您將Id屬性關閉。

UPDATE

每討論的意見,問題是,你是應用[JsonProperty]屬性爲Id財產。由於Id屬性表示文檔關鍵字,並且未作爲JSON文檔的一部分進行序列化,因此無法將[JsonProperty]屬性應用於該屬性。

+0

今天試過。結果仍然是一樣的。 BTW。更新原始帖子以反映更改。 – azzlack 2013-04-07 12:27:30

+0

您的測試通過了我。完全如此。你使用的是什麼版本/內部版本? – 2013-04-07 17:22:05

+0

我使用的版本是2330. – azzlack 2013-04-08 09:15:46

相關問題