Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we ensure Data integrity in mongoDb?

Tags:

mongodb

nosql

i am trying to migrate from relational database (mysql) data to nosql (mongoDb) . But how can i ensure data integrity in mongodb . what i have found that we cannot do it on server side. what should i use on application side to handle data integrity ?

For eg: i have two tables user and task . Both have userId field common . if i add a new entry in task table it should check if userid present in user table. this is one of the requirement others like adding constraints , updating values etc

like image 727
rahul Avatar asked Oct 01 '15 10:10

rahul


People also ask

Does MongoDB have referential integrity?

When you search for referential integrity in relation to Mongo-DB the standard response is "MongoDB does not support this". The standard explanation is that MongoDB supports refs and populate, however there is nothing that prevents you changing the ref to an invalid value.

Which method is used to check the integrity data?

Error checking and validation, for example, are common methods for ensuring data integrity as part of a process.


2 Answers

Ultimately, you're screwed. There's no way (in mongodb) to guarantee data integrity in such scenario, since it's lacking relations in general and foreign keys in particular. And there's little point in building application-level checks. No matter how elaborate they are, they can still fail (hence "no guarantee").

So it's either embedding (so that related data is always there, right in the document) or abandoning the hope of consistent data.

like image 50
Sergio Tulentsev Avatar answered Sep 24 '22 00:09

Sergio Tulentsev


  • MongoDb is nosql and hence no joins.
  • Data is stored as BSON documents and hence no Foreign key constraints

Steps to ensure Data Integrity:

  • Check in the application before adding the task document whether it is having a valid user.
like image 21
Clement Amarnath Avatar answered Sep 23 '22 00:09

Clement Amarnath