使用 Ravendb (build 960) 我正在尝试对一组文档执行批量更新,以替换字符串列表中的单个值。我使用Google Group Question作为代码的基础,因为请求几乎相同,但由于某种原因,他们能够在我的错误出现时让他们的问题正常工作。我编写了以下示例控制台应用程序来演示该问题。
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”。每次都出现相同的错误,并且由于字符串列表似乎没有名称,因此我故意在上面省略了它。
提前致谢。