diff --git a/README.md b/README.md index 57a7206..63e082d 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,18 @@ msg = { Notice that the payload from the `deadQueue` is exactly the same as the original message when it was on the original queue (except with the number of tries set to 5). +### returnDocument ### + +The `mongodb` Node.js driver [deprecated](https://github.com/mongodb/node-mongodb-native/pull/2808) +use of `returnOriginal` in favor of `returnDocument` when using `findOneAndUpdate()`. + +If you want to opt in to using the newer `returnDocument`, set the `returnDocument` option +to `true`: + +``` +var queue = mongoDbQueue(db, 'queue', { returnDocument : true }) +``` + ## Operations ## ### .add() ### diff --git a/mongodb-queue.js b/mongodb-queue.js index a712f1c..6c82d29 100644 --- a/mongodb-queue.js +++ b/mongodb-queue.js @@ -44,6 +44,7 @@ function Queue(db, name, opts) { this.col = db.collection(name) this.visibility = opts.visibility || 30 this.delay = opts.delay || 0 + this.returnDocument = opts.returnDocument if ( opts.deadQueue ) { this.deadQueue = opts.deadQueue @@ -120,8 +121,11 @@ Queue.prototype.get = function(opts, callback) { visible : nowPlusSecs(visibility), } } + var options = self._optionsWithNewDocument({ + sort: sort + }) - self.col.findOneAndUpdate(query, update, { sort: sort, returnOriginal : false }, function(err, result) { + self.col.findOneAndUpdate(query, update, options, function(err, result) { if (err) return callback(err) var msg = result.value if (!msg) return callback() @@ -175,7 +179,9 @@ Queue.prototype.ping = function(ack, opts, callback) { visible : nowPlusSecs(visibility) } } - self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) { + var options = self._optionsWithNewDocument({}) + + self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) { if (err) return callback(err) if ( !msg.value ) { return callback(new Error("Queue.ping(): Unidentified ack : " + ack)) @@ -197,7 +203,8 @@ Queue.prototype.ack = function(ack, callback) { deleted : now(), } } - self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) { + var options = self._optionsWithNewDocument({}) + self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) { if (err) return callback(err) if ( !msg.value ) { return callback(new Error("Queue.ack(): Unidentified ack : " + ack)) @@ -266,3 +273,12 @@ Queue.prototype.done = function(callback) { callback(null, count) }) } + +Queue.prototype._optionsWithNewDocument = function(query) { + if (this.returnDocument) { + query.returnDocument = 'after' + } else { + query.returnOriginal = false + } + return query +}