Having no constraints in a NoSQL database means complete data flexibility. This sounds appealing at first given the fact that it saves development time by not defining any columns or data types at the time of inception of your collections. But this much flexibility comes at a cost, especially when your database grows large enough and some of the documents may contain bad values or missing attributes, difficult to find from queries. This, in turn, might change the result set of your queries and ultimately hurt your business decisions.
On the other hand a relational database requires defined tables and columns before you start accessing your database. Because this data is stored in a strict format, there is no possibility of any bad values or missing attributes and returns accurate query results. Of course, ensuring strict format rules pretty much completely kills any data flexibility you had and slows down the ability to add and therefore query, new data.
Constraining data flexibility
But fret not you can have the best of both worlds by using NoSQL triggers. In NosDB, a .NET based NoSQL Document Database, you can use triggers to validate your data and enforce data rules ensuring data format consistency no matter how huge your database becomes. Triggers are functions registered against a database operation. They are executed in response to the operation being performed, hence ‘triggering’ the function.
To use, simply create a .NET class and implement Alachisoft.NosDB.Common.Triggers.IDatabaseTrigger from the NosDB Common library found in the installation folder. There are two methods available but we’ll be using the PreTrigger method only and leave the PostTrigger method as is, since we just want to validate our data before adding it in the collection. Here is example code:
This code ensures two things.
- Return ‘false’ to reject object if we have a missing attribute.
- Update a JSON Document, if it’s missing, with a default value and return ‘true’ to accept the data.
We could also check if an attribute is of the exact datatype or not, and reject the data entry as needed.
To register this trigger with a collection, open up the NosDB Management Studio. Connect with a cluster and select a collection from a database. Expand up to the ‘Collections’ and bring up the context menu by right clicking on CLR Triggers. Follow the trigger registration wizard. Take a look at the following figure for a better idea:
Since we are only interested in validating the data on every insert (as shown from the code above) you only need to register the PreInsert Trigger. And that’s it!
NosDB is Open Source and 100% native .NET NoSQL Database (released under the Apache 2.0 License). NosDB is super fast and linearly scalable, allowing your .NET applications to handle extreme transaction loads (XTP), and it works in Visual Studio.
NosDB also helps you accelerate .NET development by providing a flexible JSON schema. With JSON, you can quickly adjust to your changing data requirements, thereby lowering your time to market.