Domanda

  • Does MongoDB handle consistency for linked documents that come from a TTL collection ?

e.g: If the "User" model is referring to a TTL collection and one of the linked users is removed for being too old, will it's links to various groups be removed ?

var GroupSchema = new Schema({
    users           : [{
        type        : Schema.Types.ObjectId, ref: 'User'
    }]
});

mongoose.model('Group', GroupSchema);
  • If not, what are the best practices for handling lists of linked documents that were removed due to TTL ?

In CouchDB you can listen to events triggered by I/O operations I think, are there any similar mechanisms in place or being implemented currently for MongoDB ?

If it matters, I'm using the Node.js Mongoose ODM.

Thank you in advance !

È stato utile?

Soluzione

MongoDB does not enforce referential integrity.

MongoDB does not have triggers.

When a document gets deleted, it is the responsibility of the application to make sure that any referenced documents are also deleted and any references pointing to it are removed or updated. When the end of a documents TTL is an event which requires to do more than just deleting that one document, the TTL functionality on the database is insufficient for the job and you need to create a different solution on the application-level.

By the way: When an object owns many children which are meaningless without it (a case of composition, not aggreagation), it usually makes sense in MongoDB to embed the children in the parent-document instead of having them as separate documents. That way they are deleted together with the parent.

Also, you should always ask yourself if it is really necessary to really delete data and if it wouldn't be more wise to just mark it as deleted by setting a flag on it. In case of a user-error, removing a delete-flag is easy, but reverting a hard delete on the database is not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top