【问题标题】:RavenDB Patch API: updating a nested collectionRavenDB Patch API:更新嵌套集合
【发布时间】:2013-03-30 03:19:16
【问题描述】:

我正在尝试使用 Patch API 更新嵌套集合。更具体地说,考虑以下示例 - 一个 Posts 集合:

{
  "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-updateshttp://ravendb.net/docs/client-api/set-based-operations 的 Patch 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);

【问题讨论】:

    标签: ravendb patch


    【解决方案1】:

    现在可以使用scripted patch request

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

    【讨论】:

    • 我会使用索引按评论标题查询,以加快补丁请求;在 JS 中查询比过滤所有索引结果要快。
    【解决方案2】:

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

    【讨论】:

    • 谢谢 Ayende 我不知道。我删除了 prevVal 并添加了 Position =0 并保留了“new IndexQuery()”,但该值仍未更新。还有什么需要改变的吗?我在原帖中添加了编辑后的补丁命令。
    • 我刚刚使用了“new IndexQuery{}”,现在一切正常。谢谢!
    猜你喜欢
    • 2019-05-11
    • 2016-02-13
    • 2021-12-05
    • 2021-11-04
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多