From 77eef29f6886b2b6fee5b8e8d69b78af44eee16d Mon Sep 17 00:00:00 2001 From: saintedlama Date: Wed, 9 Dec 2015 15:14:44 +0100 Subject: [PATCH] Allow passing opts including writeOps to save --- README.md | 2 +- lib/collection.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a048812..50f5ac9 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ Apply a query and get one single document passed as a callback. The callback rec #####`db.collection.runCommand(command, [callback])` -#####`db.collection.save(doc, [callback])` +#####`db.collection.save(doc, [options], [callback])` #####`db.collection.stats(callback)` diff --git a/lib/collection.js b/lib/collection.js index a5d6ac2..26b0545 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -107,16 +107,18 @@ Collection.prototype.update = function (query, update, opts, cb) { }) } -Collection.prototype.save = function (doc, cb) { - if (!cb) { return this.save(doc, noop) } +Collection.prototype.save = function (doc, opts, cb) { + if (!opts && !cb) return this.save(doc, {}, noop) + if (typeof opts === 'function') return this.save(doc, {}, opts) + if (!cb) return this.save(doc, opts, noop) if (doc._id) { - this.update({_id: doc._id}, doc, {upsert: true}, function (err) { + this.update({_id: doc._id}, doc, xtend({upsert: true}, opts), function (err) { if (err) return cb(err) cb(null, doc) }) } else { - this.insert(doc, cb) + this.insert(doc, opts, cb) } }