delete and references

1,417 views
Skip to first unread message

ajay

unread,
Aug 7, 2012, 12:55:59 PM8/7/12
to mongod...@googlegroups.com
I am a newbie to mongoDB and have couple of questions:

1) If document A keeps reference of document B, what happens when document B is deleted? Will A.B be "null" now or will it keep pointing to a non-existent document and its up to the application code to keep the references consistent?

2) In my application, a document may have some child documents. But I am not keeping these child documents embedded, because my application need to expose these child documents as independently filterable objects. Instead I am keeping a reference to parent document inside these child documents. Now when a parent document is removed, I need to make sure its child documents are removed too. How can I accomplish this? I read somewhere in Morphia guide that pre-delete or post-delete triggers are not supported (and for a good reason).

Thanks

-Ajay 

Octavian Covalschi

unread,
Aug 7, 2012, 1:24:38 PM8/7/12
to mongod...@googlegroups.com
You pretty much answered your questions. All this should be done at application level. It's up to your framework/code to deal with references and children...



-Ajay 

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

William Z

unread,
Aug 7, 2012, 1:36:35 PM8/7/12
to mongod...@googlegroups.com
Hi Ajay!

1) "References" in MongoDB are simply data values that uniquely identify other documents.  There is no relational consistency enforced by MongoDB: you have to handle it in the application.  So yes, you can have "dangling references" in MongoDB. 

The general design pattern is to find some way to null out the references yourself.  If the referencing field is indexed, you can easily null out all references to it with a single update() command:

  db.A.update( {B:$DeletedDocRef}, {$set: {B:null} } );

Note that this is only efficient if A.B is indexed.

2) Again, you need to handle the delete in your application code.

Assuming that you have the following schema:

  db.parent.findOne()
  { _id: 1959, name: "Ajay" }

  db.child.findOne()
  { _id: 5280, parent: 1959 }

You can delete all children of "Ajay" using the following code:

  db.child.delete( {parent: 1959} );

3)  Here are some good general references on MongoDB schema design.
 - http://www.manning.com/banker/ (MongoDB in Action)
 - http://shop.oreilly.com/product/0636920018391.do
 - http://www.10gen.com/presentations/mongosf2011/schemabasics
 - http://www.10gen.com/presentations/mongosv-2011/schema-design-by-example
 - http://www.10gen.com/presentations/mongosf2011/schemascale
Here are some sample schema designs:
 - http://docs.mongodb.org/manual/use-cases/


Let me know if you have further questions.

 -William

ajay

unread,
Aug 7, 2012, 1:56:10 PM8/7/12
to mongod...@googlegroups.com
Thank you so much William for your detailed reply.  It would be nice if there was a way to get a trigger/callback in the application code when parent gets removed. Because parent remove may not always be application initiated. It could be, for example, done from Mongo shell interface.

Sam Millman

unread,
Aug 7, 2012, 2:59:05 PM8/7/12
to mongod...@googlegroups.com
There is a JIRA for this: https://jira.mongodb.org/browse/SERVER-124

--

William Z

unread,
Aug 7, 2012, 5:08:44 PM8/7/12
to mongod...@googlegroups.com

Hi Ajay!

As Sammaye mentioned, there is an existing Jira request for database triggers, which you can watch, comment on, and vote up. 

In the interim, you can implement a "data integrity checker" process that runs intermittently, walks through the content of a collection, and checks the data integrity for that collection. 

For more information about the design philosophy behind MongoDB, take a look at this presentation by Dwight Merriman, one of the co-founders of MongoDB:

 - http://www.10gen.com/presentations/mongosf2011/whymongodb



Let me know if you have further questions.

 -William



Reply all
Reply to author
Forward
0 new messages