Over a million developers have joined DZone.
Gold Partner

Better patching API for RavenDB: Creating New Documents

· Database Zone

The Database Zone is brought to you in partnership with Clusterpoint.  Discover how ACID is a set of properties that guarantee the reliable processing of data through use of transactions by downloading this whitepaper.

A while ago we introduced the ability to send js scripts to RavenDB for server side execution. And we have just recently completed a nice improvement on that feature, the ability to create new documents from existing ones.

Here is how it works:

store.DatabaseCommands.UpdateByIndex("TestIndex",
                                     new IndexQuery {Query = "Exported:false"},
                                     new ScriptedPatchRequest { Script = script }
  ).WaitForCompletion();

Where the script looks like this:

for(var i = 0; i < this.Comments.length; i++ ) {
   PutDocument('comments/', {
    Title: this.Comments[i].Title,
    User: this.Comments[i].User.Name,
    By: this.Comments[i].User.Id
  });
}

this.Export = true;

This will create a set of documents for each of the embedded documents.

The Database Zone is brought to you in partnership with Clusterpoint.  Discover how ACID is a set of properties that guarantee the reliable processing of data through use of transactions by downloading this whitepaper.

Topics:

Published at DZone with permission of Ayende Rahien , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}