Musical Shore Musical Shore - 1 year ago 67
Node.js Question

"Race like" condition with Mongoose

I have a process that triggers a number of requests that in turn trigger off a number of webhooks. I know the process is complete when I've received all of my webhooks. My model looks something like this.

name: 'name',
status: 'PENDING',
children: [{
name: 'child1',
status: 'PENDING'
}, {
name: 'child2',
status: 'PENDING'


The idea is as the webhooks come in, I update the subdoc to
. At the end of each update I check if the others are complete and if they are, I set
on the parent. It looks like one invocation of the service is marking it as
after another invocation has determined it was still
but before that second invocation has saved. When the second one saves, it overwrites
on the parent.

Here is the code from my schema:

methods: {
doUpdate: function(data) {
var child =;
child.status = 'COMPLETE';
if (_.every(this.children.status, 'COMPLETE'))
this.status = 'COMPLETE';

Answer Source

The solution is to first find and update the status in one operation, using a variant of mongo's findAndModify method, and then check if the other children have completed. I was trying to do two updates in one operation. By breaking it up into two steps, I know that when I check the statuses of the other children, all other invocations will have the most recent state of the document and not get a false reading while another invocation is waiting for save() to complete.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download