How can we ensure data integrity in mongoDb?

I am trying to transfer relational database (mysql) data to nosql (mongoDb). But how can I ensure data integrity in mongodb. what I found is that we cannot do this on the server side. What should I use on the application side to process data integrity?

For example, I have two users and a table task. Both have a userId field. if I add a new entry to the task table, she should check if the user ID is present in the user table. this is one of the requirements that others require adding constraints, updating values, etc.

+5
source share
3 answers

Ultimately, you are screwed. There is no way (in mongodb) to guarantee data integrity in such a scenario, since it lacks relationships in general and foreign keys in particular. And a small point in building application level checks. No matter how complicated they are, they can still fail (hence the “no guarantee”).

Thus, this is either an attachment (so that there is always related data, right in the document) or a rejection of the hope for consistent data.

+5
source

MongoDB does not support FOREIGN KEY. It uses to avoid JOINS.

MongoDB does not support server-side foreign key relationships. But sometimes we need to tie. Therefore, MongoDB applications use one of two methods for linking documents:

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

  • DBRefs are links from one document to another, using the value of the firstid documents field, the name of the collection, and, if necessary, its database name. Including these names, DBRefs allow documents located in multiple collections to be more easily linked to documents from the same collection. This may not be so fast then, because the database must make additional queries to read objects, but it allows the appearance of a link to a foreign key. However, you will have to manually process your links. Just looking at your DBRef, you will see if it exists, the database will not go through all the documents to look for links and delete them if the link’s purpose no longer exists. But I think that deleting all the links after deleting the book will require a separate collection request, no more, so it's not that difficult.

See the documentation: Database Links for more information .

How can I solve this problem?

To be clear, MongoDB is not relational. There is no standard “normal form”. You must model your database that matches the stored data and the queries you intend to use. 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 solve this problem.

 student { _id: ObjectId(...), name: 'Jane', courses: [ { name: 'Biology 101', mark: 85, id:bio101 }, ] } 
+2
source
  • MongoDb is nosql and therefore not combined.
  • Data is stored as BSON documents and therefore does not have foreign key restrictions

Steps to ensure data integrity:

  • Verify the application before adding the task document to see if it has a valid user.
+2
source

Source: https://habr.com/ru/post/1232696/


All Articles