PatchAPIを使用してネストされたコレクションを更新しようとしています。具体的には、次の例を検討してください-投稿コレクション:
{
"Title": "Hello RavenDB",
"Category": "RavenDB",
"Content": "This is a blog about RavenDB",
"Comments": [
{
"Title": "Unrealistic",
"Content": "This example is unrealistic"
},
{
"Title": "Nice",
"Content": "This example is nice"
}
]
}
http://ravendb.net/docs/client-api/partial-document-updatesおよびhttp://ravendb.net/docs/client-api/set-based-でパッチAPIとセットベースの操作ドキュメントを使用しましたセット操作と静的インデックスを使用して一括更新を行うためのリソースとして、操作といくつかのスタックオーバーフローの質問。以前の値が「Nice」の場合にのみコメントの「Title」を更新し、その場合は「Bad」に更新する必要があります。
静的インデックス「NicePosts」は次のように定義されます。
Map = posts => from post in posts
where post.Comments.Any(comment => comment.Title == "Nice")
select new {post.Title, post.Category}
一括パッチ更新コマンドは次のとおりです。
documentStore.DatabaseCommands.UpdateByIndex("NicePosts",
new IndexQuery(),
new[] { new PatchRequest
{ Type = PatchCommandType.Modify,
Name = "Comments",
PrevVal = RavenJObject.Parse(@"{ ""Title"": ""Nice""}"),
Nested = new[]
{
new PatchRequest {Type = PatchCommandType.Set, Name = "Title", Value = new RavenJValue("Bad") },
} }, allowStale: true);
これに関していくつか質問があります:
1)更新コマンドの構造/構文は正しいですか?
2)コレクション内のすべてのレコードに対して更新を実行したい。したがって、「NicePosts」インデックスはすでに適切なセットを返しているため、IndexQueryクエリでクエリフィルタを定義していません。ただし、このコマンドを実行してもコレクションは更新されません。
3)「allowStale:false」を設定すると、「staleindex」エラーが発生します。ドキュメントストアセッションを開く前に、インデックスクラスをインスタンス化し、それを実行してravenDBインスタンスに永続化します。ここで何がうまくいかないのですか?
ありがとう、
編集:
ayendeの推奨に基づいて、パッチコマンドを次のように変更しました。
documentStore.DatabaseCommands.UpdateByIndex("NicePosts",
new IndexQuery(),
new[] {
new PatchRequest {
Type = PatchCommandType.Modify,
Name = "Comments",
Position = 0,
Nested = new[] {
new PatchRequest {Type = PatchCommandType.Set, Name = "Title", Value = new RavenJValue("Bad")},
}
}
}, allowStale: false);