RavenDB 2.x  Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – patching a document

You will learn how to patch the document with the ID Orders/A655302 using a patching script that combines the JavaScript and parameters.

  1. In the Management Studio, select the Orders database to be the current database and click on the Patch tab to show the Patch screen.
  2. In the Patch type list, select the Document type.
  3. In the Document to patch textbox, enter Orders/A655302, which is the ID of the document we want to patch, and press Enter.
  4. Click on the Add new Parameter button (the green plus sign to the right of the Parameters section) to add a new parameter.
  5. Enter ItemName for the Name and Ink Pen for the Value.
  6. In the Patch Scripts zone, enter the following JavaScript code snippet:
    this.UnitCost = this.UnitCost * 1.15
    this.Item = ItemName
  7. Click on the Test button to test the patch script on the selected document.
  8. Observe the changes in the After Patch section and verify that the document has new fields value.
  9. Click on the Patch button to execute the patch scripts to make the changes permanent.

What just happened?

We learned how to patch a document using the Management Studio.

We patched the document with the ID= Orders/A655302 to increase the UnitCost field value by 15 percent and change the Item field value from Pen to Ink Pen.

In steps 1 to 3, we opened the Management Studio and displayed the Patch screen. Then we entered the ID of the document that we want to patch.

In steps 4 to 6, we added a new parameter with name= ItemName and value=Ink Pen and entered the patching script.

In this JavaScript code snippet, in the first line of code, the UnitCost property value is increased by 15 percent. In the second line, the value of the parameter named ItemName will be assigned to the Item property.

In steps 7 and 8, we tested the patching script before applying it and visualized the result. Finally, in step 9, we executed the patch script to make the changes permanent.

Have a go hero – patching the Orders collection

In the previous section, you learned to patch a single document. Suppose in a migration scenario when significant structural changes are required by the consuming application, you need to patch more than one document. Of course, you will not process one document at a time. Try to patch the Orders collection and increase all Units values by 10 percent.