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 Google Group Question 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.
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);
}
}
When I run I get:
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
While I believe the believe the process is correct I must be missing something otherwise it wouldn’t be erroring out.
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.
Thanks in advance.
I’ve been looking how to do the same thing recently. It sounds like it isn’t possible to use the current patching API to change a string array.
See the discussion I’ve been having here: https://groups.google.com/forum/#!topic/ravendb/5qYWsq_ny0M