【问题标题】:Add a field to a document in raven 3.5 where the property doesn't exist在属性不存在的 raven 3.5 中向文档添加字段
【发布时间】:2021-11-17 11:50:52
【问题描述】:

在我的 raven3.5 实例中,我有一个集合,并且由于文档结构的更改,一些文档具有额外的属性。现在我需要查询该属性,但许多旧文档没有它。

如何修补集合并将属性添加到没有它的文档?或者创建一个查询来获取不具有特定属性的文档?

谢谢。

【问题讨论】:

    标签: ravendb ravendb-studio ravendb-3.5


    【解决方案1】:

    以下是我所知道的适用于 RavenDB Server 4.2 及更高版本

    选项 1 - 使用静态索引

    使用以下 map 方法创建一个 static index('MyIndex'):

    from i in docs.Items
    select new {
        Name = i.Name
    }
    

    然后查询索引如下:

    返回所有没有“名称”的“项目”:

    from index 'MyIndex'
    where true and not exists(Name)
    

    选项 2 - 使用自动索引:

    首先,运行以下查询,它将为您“创建”auto-index

    from Items
    where exists(Name) 
    

    之后,您可以查询以下内容以获取结果:

    from Items
    where true and not exists(Name)
    

    ================================================ ==

    patch 脚本由查询部分更新部分
    组成 因此,使用以下方法将缺少的属性添加到没有它的文档中:

    from Items
    where true and not exists(Name)
    update {
       this.Name = "value"
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2011-09-26
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      相关资源
      最近更新 更多