Question

Before saving a new document to a mongodb collection via nodejs in my MongoLab database, I'm using model.count to check certain fields to prevent a duplicate entry:

MyModel.count({field1: criteria1, field2: criteria2}, function (err, count) {
  if (count == 0) {
    // Create new document and call .save()
  }
});

However, during testing I'm noticing many duplicates (inconsistent in number across test runs) in the collection after the process finishes, although not as many as if I did not do the .count() check.

Since the MyModel.count() statement is embedded in a callback being repeatedly called whenever the 'readable' event is emitted by one of several ReadStreams, I suspect there is an async issue caused by rapid writes to the collection. Specifically, two or more identical and nearly simultaneous calls to MyModel.count return a count of 0, and end up each creating and saving (identical) documents to the collection.

Does this sound probable? And if so how can I enforce uniqueness of document writes without setting timeouts or using a synchronous pattern?

Was it helpful?

Solution

As Peter commented, the right way to enforce uniqueness is to create a unique index on the collection over those fields and then handle the code: 11000 insert error to recover from attempts at creating duplicates.

You can add the index via your schema before you create the model from it:

mySchema.index({field1: 1, field2: 1}, {unique: true});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top