Previous posts on the NoSql and Raven Series
- NoSql and a life without Schema
- NoSql and a life without schema continued
- Rename a property in RavenDb
- First Touch of RavenDb HTTP API
Other posts by Mauro on RavenDb Subject.
In third post of the series, I dealt with renaming of properties in an entity that has already some instances saved in RavenDB. The solution proposed was based on PATCH commands issued by C# code based on an Index created with RavenStudio, but now that I showed you how to issue HTTP API request, it is time to understand how to issue set-based PATCH commands to modify multiple documents with simple HTTP call.
First of all I want to refresh the concept that a Set-Based operation operates on indexes, thus if you want to PATCH multiple documents at once, you should create an index that identify all the documents you want to PATCH. We already saw how to create an index that identify all Documents of a certain type with C#, but if you want to create an index with HTTP api you can simple issue this Curl command
the –X PUT is used to specify a PUT request, used to store something in database, the address identifies the name of the index and should respect the mask: http://xxx.xxx.xxx.xxx:port/indexes/nameoftheindex, –d option identify the body of the request, and it is absolutely equal to the one showed in previous post from RavenDbStudio. The advantage of creating the index with HTTP API is the ability to include it in a simple Batch or script command, without the need to pass from the UI of RavenDbStudio.
Now you can issue the PATCH command to operate on all entities that satisfy the index.
The HTTP API command to PATCH documents based on an index is really simple, just issue a PATCH call specifying the index to use and the payload should contain the details of PATCH operation, as described in the documentation. If the index was created only to issue the PATCH command, you can now safely delete it with this call
Thanks to three command line HTTP API calls you are able to rename a property for all documents of a certain type in a RavenDb from a simple batch script, that you can include for example in your software setup procedure to update all documents in case of a Property Rename.