rahul
rahul

Reputation: 239

How can we ensure Data integrity in mongoDb?

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

Upvotes: 18

Views: 15634

Answers (3)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

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.

Upvotes: 13

Hitesh Mundra
Hitesh Mundra

Reputation: 1588

MongoDB doesn't support FOREIGN KEY. It's uses to Avoid JOINS.

MongoDB doesn't support server side foreign key relationships. But some times we need to relate So MongoDB applications use one of two methods for relating documents:

  1. Manual references where you save the _id field of one document in another document as a reference. Then your application can run a second query to return the related data. These references are simple and sufficient for most use cases.

  2. DBRefs are references from one document to another using the value of the first document’s _id field, collection name, and, optionally, its database name. By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection.This may be then not so speedy because DB has to make additional queries to read objects but allows for kind of foreign key reference.Still you will have to handle your references manually. Only while looking up your DBRef you will see if it exists, the DB will not go through all the documents to look for the references and remove them if the target of the reference doesn't exist any more. But I think removing all the references after deleting the book would require a single query per collection, no more, so not that difficult really.

Refer to documentation for more info: Database References.

How can I solve this task?

To be clear, MongoDB is not relational. There is no standard "normal form". You should model your database appropriate to the data you store and the queries you intend to run. For ex-

student
{ 
  _id: ObjectId(...),
  name: 'Jane',
  courses: [
    { course: 'bio101', mark: 85 },
    { course: 'chem101', mark: 89 }
  ]
}

course
{
  _id: 'bio101',
  name: 'Biology 101',
  description: 'Introduction to biology'
}

Try to resolve to this

student
{ 
    _id: ObjectId(...),
    name: 'Jane',
    courses: [
    { 
        name: 'Biology 101', 
        mark: 85, 
        id:bio101 
    },
  ]
}

Upvotes: 2

Clement Amarnath
Clement Amarnath

Reputation: 5466

  • 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.

Upvotes: 3

Related Questions