0
  • 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 !

shingara
  • 46,608
  • 11
  • 99
  • 105
m_vdbeek
  • 3,704
  • 7
  • 46
  • 77
  • MongoDB has no joins server-side as such no, it does not. As for best practice, what stops you from nesting the document if it all is useless if that original document is removed? – Sammaye Oct 21 '13 at 09:11
  • What do joins have to do with my question ? The issue is related to the whereabouts of links to removed documents of TTL collections. – m_vdbeek Oct 21 '13 at 09:16
  • JOINs would be required to ensure referential integrity of your linked documents – Sammaye Oct 21 '13 at 09:20

1 Answers1

1

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.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • But embedding wouldn't be a solution in the case of TTL collections because you would have to chose between losing that TTL feature and deleting the entire group, right ? – m_vdbeek Oct 21 '13 at 10:18
  • @m_vdbeek When a group doesn't own its members and it doesn't make sense to delete them together with the group, then you should not embed them. – Philipp Oct 21 '13 at 11:05