Skip to content

Commit 0e11c97

Browse files
authored
Merge pull request #3 from reedsy/return-document
👽 Add option to use `returnDocument`
2 parents 5f0d9ab + 804fa22 commit 0e11c97

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ msg = {
237237
Notice that the payload from the `deadQueue` is exactly the same as the original message
238238
when it was on the original queue (except with the number of tries set to 5).
239239

240+
### returnDocument ###
241+
242+
The `mongodb` Node.js driver [deprecated](https://github.com/mongodb/node-mongodb-native/pull/2808)
243+
use of `returnOriginal` in favor of `returnDocument` when using `findOneAndUpdate()`.
244+
245+
If you want to opt in to using the newer `returnDocument`, set the `returnDocument` option
246+
to `true`:
247+
248+
```
249+
var queue = mongoDbQueue(db, 'queue', { returnDocument : true })
250+
```
251+
240252
## Operations ##
241253

242254
### .add() ###

mongodb-queue.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function Queue(db, name, opts) {
4444
this.col = db.collection(name)
4545
this.visibility = opts.visibility || 30
4646
this.delay = opts.delay || 0
47+
this.returnDocument = opts.returnDocument
4748

4849
if ( opts.deadQueue ) {
4950
this.deadQueue = opts.deadQueue
@@ -120,8 +121,11 @@ Queue.prototype.get = function(opts, callback) {
120121
visible : nowPlusSecs(visibility),
121122
}
122123
}
124+
var options = self._optionsWithNewDocument({
125+
sort: sort
126+
})
123127

124-
self.col.findOneAndUpdate(query, update, { sort: sort, returnOriginal : false }, function(err, result) {
128+
self.col.findOneAndUpdate(query, update, options, function(err, result) {
125129
if (err) return callback(err)
126130
var msg = result.value
127131
if (!msg) return callback()
@@ -175,12 +179,13 @@ Queue.prototype.ping = function(ack, opts, callback) {
175179
visible : nowPlusSecs(visibility)
176180
}
177181
}
182+
var options = self._optionsWithNewDocument({})
178183

179184
if (opts.resetTries) {
180185
update.$set.tries = 0
181186
}
182187

183-
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
188+
self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
184189
if (err) return callback(err)
185190
if ( !msg.value ) {
186191
return callback(new Error("Queue.ping(): Unidentified ack : " + ack))
@@ -202,7 +207,8 @@ Queue.prototype.ack = function(ack, callback) {
202207
deleted : now(),
203208
}
204209
}
205-
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
210+
var options = self._optionsWithNewDocument({})
211+
self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
206212
if (err) return callback(err)
207213
if ( !msg.value ) {
208214
return callback(new Error("Queue.ack(): Unidentified ack : " + ack))
@@ -271,3 +277,12 @@ Queue.prototype.done = function(callback) {
271277
callback(null, count)
272278
})
273279
}
280+
281+
Queue.prototype._optionsWithNewDocument = function(query) {
282+
if (this.returnDocument) {
283+
query.returnDocument = 'after'
284+
} else {
285+
query.returnOriginal = false
286+
}
287+
return query
288+
}

0 commit comments

Comments
 (0)