2012-07-11 70 views
1

使用Ravendb(內部版本960)我試圖對一組文檔執行批量更新以替換字符串列表中的單個值。我使用Google Group Question作爲代碼的基礎,因爲請求幾乎是相同的,但由於某些原因,他們能夠讓我們的工作,而我的錯誤了。我編寫了下面的示例控制檯應用程序來演示該問題。Ravendb - 修補字符串列表

public class Document 
{ 
    public const string OLD_NAME = "Label A"; 
    public const string NEW_NAME = "Label B"; 

    public Document() 
    { 
     Labels = new List<string> { OLD_NAME }; 
    } 

    public string Id { get; set; } 
    public IList<string> Labels { get; set; } 
} 

public class Document_By_Labels : AbstractIndexCreationTask<Document> 
{ 
    public Document_By_Labels() 
    { 
     Map = leads => from doc in leads select new {doc.Labels}; 
    } 
} 

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     IDocumentStore store = new DocumentStore 
     { 
      Url = "http://localhost:8081", 
      DefaultDatabase = "RavendbPatchStringListTest" 
     }.Initialize(); 
     IndexCreation.CreateIndexes(typeof (Program).Assembly, store); 

     using (IDocumentSession session = store.OpenSession()) 
     { 
      var s = new Document(); 
      session.Store(s); 
      session.SaveChanges(); 

      var d = session.Load<Document>(s.Id); 
      var m = session.Advanced.GetMetadataFor(d); 
     } 

     store.DatabaseCommands.UpdateByIndex("Document/By/Labels", 
              new IndexQuery {Query = string.Format("Labels:\"{0}\"", Document.OLD_NAME)}, 
              new[] 
              { 
               new PatchRequest 
               { 
                Type = PatchCommandType.Modify, 
                Name = "Labels", 
                AllPositions = true, 
                Nested = 
                 new[] 
                 { 
                  new PatchRequest 
                  { 
                   Type = PatchCommandType.Remove, 
                   Value = new RavenJValue(Document.OLD_NAME) 
                  }, 
                  new PatchRequest 
                  { 
                   Type = PatchCommandType.Add, 
                   Value = new RavenJValue(Document.NEW_NAME) 
                  } 
                 } 
               } 
              }, allowStale: true); 
    } 
} 

當我運行我得到:

System.InvalidCastException: Unable to cast object of type 'Raven.Json.Linq.RavenJValue' to type 'Raven.Json.Linq.RavenJObject'. 
    at Raven.Json.Linq.Extensions.Convert[U](RavenJToken token, Boolean cast) in c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\Extensions.cs:line 131 
    at Raven.Json.Linq.Extensions.Convert[U](RavenJToken token) in c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\Extensions.cs:line 116 
    at Raven.Json.Linq.Extensions.Value[U](RavenJToken value) in c:\Builds\RavenDB-Stable\Raven.Abstractions\Json\Linq\Extensions.cs:line 24 
    at Raven.Database.Json.JsonPatcher.ModifyValue(PatchRequest patchCmd, String propName, RavenJToken property) in c:\Builds\RavenDB-Stable\Raven.Database\Json\JsonPatcher.cs:line 138 
    at Raven.Database.Json.JsonPatcher.Apply(PatchRequest patchCmd) in c:\Builds\RavenDB-Stable\Raven.Database\Json\JsonPatcher.cs:line 61 
    at Raven.Database.Json.JsonPatcher.Apply(PatchRequest[] patch) in c:\Builds\RavenDB-Stable\Raven.Database\Json\JsonPatcher.cs:line 30 
    at Raven.Database.DocumentDatabase.<>c__DisplayClassc1.<ApplyPatch>b__be(IStorageActionsAccessor actions) in c:\Builds\RavenDB-Stable\Raven.Database\DocumentDatabase.cs:line 1150 
    at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:\Builds\RavenDB-Stable\Raven.Storage.Esent\TransactionalStorage.cs:line 330 
    at Raven.Database.DocumentDatabase.ApplyPatch(String docId, Nullable`1 etag, PatchRequest[] patchDoc, TransactionInformation transactionInformation) in c:\Builds\RavenDB-Stable\Raven.Database\DocumentDatabase.cs:line 1131 
    at Raven.Database.Impl.DatabaseBulkOperations.<>c__DisplayClass2.<UpdateByIndex>b__1(String docId, TransactionInformation tx) in c:\Builds\RavenDB-Stable\Raven.Database\Impl\DatabaseBulkOperations.cs:line 42 
    at Raven.Database.Impl.DatabaseBulkOperations.<>c__DisplayClassa.<PerformBulkOperation>b__5(IStorageActionsAccessor actions) in c:\Builds\RavenDB-Stable\Raven.Database\Impl\DatabaseBulkOperations.cs:line 80 
    at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action) in c:\Builds\RavenDB-Stable\Raven.Storage.Esent\TransactionalStorage.cs:line 376 
    at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:\Builds\RavenDB-Stable\Raven.Storage.Esent\TransactionalStorage.cs:line 337 
    at Raven.Database.Impl.DatabaseBulkOperations.PerformBulkOperation(String index, IndexQuery indexQuery, Boolean allowStale, Func`3 batchOperation) in c:\Builds\RavenDB-Stable\Raven.Database\Impl\DatabaseBulkOperations.cs:line 75 
    at Raven.Database.Impl.DatabaseBulkOperations.UpdateByIndex(String indexName, IndexQuery queryToUpdate, PatchRequest[] patchRequests, Boolean allowStale) in c:\Builds\RavenDB-Stable\Raven.Database\Impl\DatabaseBulkOperations.cs:line 40 
    at Raven.Database.Server.Responders.DocumentBatch.<>c__DisplayClass3.<Respond>b__0(String index, IndexQuery query, Boolean allowStale) in c:\Builds\RavenDB-Stable\Raven.Database\Server\Responders\DocumentBatch.cs:line 47 
    at Raven.Database.Server.Responders.DocumentBatch.OnBulkOperation(IHttpContext context, Func`4 batchOperation) in c:\Builds\RavenDB-Stable\Raven.Database\Server\Responders\DocumentBatch.cs:line 64 
    at Raven.Database.Server.Responders.DocumentBatch.Respond(IHttpContext context) in c:\Builds\RavenDB-Stable\Raven.Database\Server\Responders\DocumentBatch.cs:line 46 
    at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\RavenDB-Stable\Raven.Database\Server\HttpServer.cs:line 550 
    at Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\Builds\RavenDB-Stable\Raven.Database\Server\HttpServer.cs:line 316 

雖然我相信,相信這個過程是正確的,我必須失去了一些東西,否則它不會被示數出來。

請注意,上面的嵌套補丁沒有名稱,因爲我嘗試了許多不同的組合,並帶有相同的錯誤。嘗試的示例:「」,「$ values」,「Labels」。每次都出現同樣的錯誤,並且由於字符串列表似乎沒有名稱,故意將其排除在上面。

在此先感謝。

回答

1

我一直在尋找最近如何做同樣的事情。這聽起來像是不可能使用當前的修補API來更改字符串數組。

見我一直在這裏的討論:https://groups.google.com/forum/#!topic/ravendb/5qYWsq_ny0M

+0

雖然不是我一直在尋找它的答案似乎是此刻唯一的答案。謝謝。 – 2012-08-02 14:45:54