RavenDB修补程序API:更新嵌套集合

RavenDB修补程序API:更新嵌套集合,ravendb,patch,Ravendb,Patch,我正在尝试使用修补程序API更新嵌套集合。更具体地,考虑下面的示例-帖子集合: { "Title": "Hello RavenDB", "Category": "RavenDB", "Content": "This is a blog about RavenDB", "Comments": [ { "Title": "Unrealistic", "Content": "This example is unrealistic" },

我正在尝试使用修补程序API更新嵌套集合。更具体地,考虑下面的示例-帖子集合:

{
  "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"
    }
  ]
}
我使用补丁API和基于集合的操作文档以及几个stackoverflow问题作为资源,使用集合操作和静态索引进行批量更新。一个要求是只在前一个值为“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) 我希望对集合中的所有记录执行更新。因此,我没有在IndexQuery查询中定义查询过滤器,因为“NicePosts”索引已经返回了相应的集合。但是,运行此命令不会更新集合

3) 如果我设置“allowStale:false”,我会得到一个“stale index”错误。在打开文档存储会话之前,我实例化索引类并执行它以将其持久化到ravenDB实例。你知道这里出了什么问题吗

谢谢

编辑:

根据ayende的建议,将Patch命令更改为:

 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);

不能使用patch命令根据数组中的现有值更新值。
您需要指定实际位置。

现在可以使用:

string oldTitle=“Nice”;
字符串newTitle=“坏”;
documentStore.DatabaseCommands.UpdateByIndex(“NicePosts”,
新建IndexQuery(),
新的ScriptedPatchRequest
{                       
脚本=@(变量i=0;i
谢谢你,阿连德,我不知道。我删除了prevVal并添加了Position=0,并保留了“new IndexQuery()”,但该值仍没有更新。还有什么需要改变的吗?我在原来的帖子中添加了编辑过的补丁命令,我只使用了“newindexquery{}”,现在一切都正常了。谢谢我会使用一个索引来查询评论标题,以加快补丁请求的速度;在JS中查询比过滤所有索引结果更快。
string oldTitle = "Nice";
string newTitle = "Bad";

documentStore.DatabaseCommands.UpdateByIndex("NicePosts",   
    new IndexQuery(),                                               
    new ScriptedPatchRequest 
    {                       
        Script = @"for (var i = 0; i < this.Comments.length; i++)
                       if (this.Comments[i].Title == oldTitle)
                           this.Comments[i].Title = newTitle;",
        Values =
        {
            { "oldTitle", oldTitle },
            { "newTitle", newTitle },
        },
    }
);