Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Integrity importance when taking the NOSQL approach? [closed]

I am almost done working on a big application; a couple of days ago I saw Martin Fowler's talk on "NOSQL". After that I realized that I have been using Mongo as a relational db so I decided to refactor my schema. But, I have heard a lot about integrity, from a guy coming from the "RDBMS" world; I really understand how important it is, so can data integrity be achieved in a "NOSQL" engine?

To be more explicit, here is a simple example :

Let's say I have a product, inventory entities in my system,

and in my inventory I reference product_id to define the product,

and currently inventory in my mongo collection looks like this
{'inventory' :{'product_id' : '1', 'count' : 15}}

So could I store the inventory like this in my system:
{'inventory' :{'product' : {'id' : 1, 'name' : 'book'}, 'count' : 15}}
and still achieve data integrity? What if I change the name of the product _id : 1 in the product entity, then what happen - will I loop to update all inventory collections?

like image 950
AboElzooz Avatar asked Mar 04 '15 14:03

AboElzooz


1 Answers

MongoDB does not have any build-in features which ensure consistency (only exception: uniqueness constraints can be enforced with unique indexes). The responsibility to not write inconsistent data to the database is delegated to the application.

When you have redundancy in your database schema (not as deadly a sin in MongoDB as it is in a relational database), then your application needs to make sure that all documents which have a duplicate of the data-point are updated. So yes, when you change a product-name and you have that product-name in more than one document, you need to update them all.

But that doesn't necessarily mean that you need a loop. You likely can do this with an update-statement with the {multi:true} option to update the product-name in all documents of a collection at once.

Untested example:

 db.collection.update(
     {
         "inventory.product.id": "1"
     },
     {
         $set: { "inventory.product.name": "New Product Name" }
     },
     {
          multi: true
     }
);

Keep in mind that this example assumes that you have nested objects, not any arrays of nested objects. When you have arrays and only want to update those array entries which match the condition, you need to use the $ operator.

like image 177
Philipp Avatar answered Nov 09 '22 13:11

Philipp