title: Ravendb - Patching a List of Strings
body: <p>Using Ravendb (build 960) I am attempting to perform a bulk update on a set of documents to replace a single value in list of strings. I used <a href="https://groups.google.com/forum/?fromgroups#!topic/ravendb/5qYWsq_ny0M" rel="nofollow">Google Group Question</a> as base for the code, as the request was the nearly identical, but for some reason they were able to get theirs to work while mine errors out. I have composed the following sample console application to demo the issue.</p>
  
  <pre><code>public class Document
  {
      public const string OLD_NAME = "Label A";
      public const string NEW_NAME = "Label B";
  
      public Document()
      {
          Labels = new List&lt;string&gt; { OLD_NAME };
      }
  
      public string Id { get; set; }
      public IList&lt;string&gt; Labels { get; set; }
  }
  
  public class Document_By_Labels : AbstractIndexCreationTask&lt;Document&gt;
  {
      public Document_By_Labels()
      {
          Map = leads =&gt; 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&lt;Document&gt;(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);
      }
  }
  </code></pre>
  
  <p>When I run I get:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClassc1.&lt;ApplyPatch&gt;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.&lt;&gt;c__DisplayClass2.&lt;UpdateByIndex&gt;b__1(String docId, TransactionInformation tx) in c:\Builds\RavenDB-Stable\Raven.Database\Impl\DatabaseBulkOperations.cs:line 42
     at Raven.Database.Impl.DatabaseBulkOperations.&lt;&gt;c__DisplayClassa.&lt;PerformBulkOperation&gt;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.&lt;&gt;c__DisplayClass3.&lt;Respond&gt;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
  </code></pre>
  
  <p>While I believe the believe the process is correct I must be missing something otherwise it wouldn't be erroring out. </p>
  
  <p>Please note there is no name on the above nested patches as I have tried a many different combos with the same error. Examples of attempts: "", "$values", "Labels". Same error each time and as a list of strings does not seem to have a Name I left it out in the above on purpose.</p>
  
  <p>Thanks in advance.</p>