Add a field to a document in raven 3.5 where the property doesn't exist

206 Views Asked by At

In my instance of raven3.5 I have a collection and some documents have an extra property due to a change in the document structure. Now I need to query on that property but many older documents don't have it.

How can I patch the collection and add the property to documents that don't have it? Or create a query that will get the documents that don't have the specific property?

Thanks.

1

There are 1 best solutions below

0
Danielle On

The below is what I know works for RavenDB Server 4.2 and up:

Option 1 - using static index

Create a static index ('MyIndex') with the following map method:

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

Then query the index as follows:

Return all 'Items' that do not have 'Name':

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

Option 2 - using auto-index:

First, run the following query which will 'create' the auto-index for you:

from Items
where exists(Name) 

After that, you can query the following to get results:

from Items
where true and not exists(Name)

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

A patch script is made up of the query part and the update part
So use the following to add the missing property to docs that don't have it:

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