Browse Source

Allow passing opts including writeOps to save

saintedlama/travis-non-legacy
saintedlama 9 years ago
parent
commit
77eef29f68
  1. 2
      README.md
  2. 10
      lib/collection.js

2
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)`

10
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)
}
}

Loading…
Cancel
Save