On this post, let's understand how to patch data using a json string in RavenDB
Because RavenDB is a NoSql database and because it stores its data internally as Json strings, sometimes when patching, we can simplify things immensely by using common JavaScript resources.
Up to Raven 3.5 the patch window accepted JavaScript (including running fancy lodash commands). So, being JavaScript, it's also valid to declare our own functions and/or use standard JavaScript functions including JSON.parse().
The Problem
My patch required to update one property (ContentName) from another (OriginalContent). Turns out that OriginalContent is a string that may or may not contain Json data and I need to access the property Name of that - if valid - Json object.Sounds complicated? How do we do that?
The Solution
Take a look at the patch below to understand what I mean:In this case my OriginalContent property stores the generic Json string that I pulled from an integration endpoint. That property is a string and can be anything: null, empty, a valid or invalid Json string, a simple string or a complex json data structure.
And what did I do on that patch?
I created a function to simply parse that property from a string to a Json object. If parsed, my code then tries to access its Name property. Then I set my ContentName property to, if available, the parsed Name from the Json string.
See Also
- My journey to 1 million articles read
- A simple introduction to RavenDB
- An in depth review of the RavenDB Cloud
- Installing and Running RavenDB on Windows and Linux
- Running RavenDB on Docker
- Querying RavenDB Metadata
- Creating ASP.NET Core websites with Docker
- Send emails from ASP.NET Core websites using SendGrid and Azure