Sandeep Singh
Sandeep Singh

Reputation: 5191

Query on MongoDB Delete Triggers

I am a newbie for MongoDB.

Please help me with the following query:

I am using oplogs for adding Triggers for MongoDB operation. On doing insert/update operations, I receive the complete information about all fields added/updated in the collection.

My problem is:

When I do delete operations in MongoDB, the oplog triggers received contains ONLY the object_id.

Can somebody please point out to some example where I can receive complete information in all fields - for the deleted row in the trigger.

Thanks

Upvotes: 2

Views: 1902

Answers (1)

Adam Comerford
Adam Comerford

Reputation: 21692

You have to fetch that document by its ObjectID, which will not be possible on the current node you are tailing the oplog from because by the time you have received the delete operation from the oplog, the document is gone. Which I believe means you have two choices:

  1. Make sure that all deletes are preceded by an update operation which allows you to see the document fields you require prior to deletion (this will make deletes more expensive of course)
  2. Run a secondary with a slave delay and then query that node for the document that has been deleted (either directly or by using tags).

For number 2, the issue is having a delay that is long enough to guarantee that you can fetch the document and short enough to make sure you are getting an up to date version of the document. Unless you add versioning to the document as a check (which is then getting similar to option 1, you would likely want to update the version before deleting), this would have to be essentially an optimistic, best efforts solution.

Upvotes: 2

Related Questions