Skip to content

Commit 6642af7

Browse files
committed
add .set(obj) and .inc(obj) support
1 parent cf3543c commit 6642af7

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lib/utils.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ exports.updateify = function (constructor, fname) {
8484
constructor.prototype[$op] = function (key, value) {
8585
if (fname) this[fname]()
8686
var document = this.document
87-
var changes = document[$op] = document[$op] || {}
88-
changes[key] = value
87+
if (typeof key === 'object') {
88+
document[$op] = key
89+
} else {
90+
var changes = document[$op] = document[$op] || {}
91+
changes[key] = value
92+
}
8993
return this
9094
}
9195
// only set without a $ if it doesn't overwrite another property
@@ -98,8 +102,12 @@ exports.updateify = function (constructor, fname) {
98102
constructor.prototype.inc = function (key, value) {
99103
if (fname) this[fname]()
100104
var document = this.document
101-
var changes = document.$inc = document.$inc || {}
102-
changes[key] = value == null ? 1 : value
105+
if (typeof key === 'object') {
106+
document.$inc = key
107+
} else {
108+
var changes = document.$inc = document.$inc || {}
109+
changes[key] = value == null ? 1 : value
110+
}
103111
return this
104112
}
105113

test/update.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ describe('.find().update()', function () {
3030
})
3131
})
3232

33+
it('.set(obj)', function () {
34+
return collection.find().set({
35+
obj: true
36+
}).then(function () {
37+
return collection.find()
38+
}).then(function (docs) {
39+
assert(docs.length > 1)
40+
assert(docs.every(function (doc) {
41+
return doc.obj = true
42+
}))
43+
})
44+
})
45+
3346
it('.set(key, value)', function () {
3447
return collection.find().set('asdf', 3).then(function () {
3548
return collection.find()
@@ -190,6 +203,16 @@ describe('.find().inc()', function () {
190203
assert.equal(7, doc.qqqq)
191204
})
192205
})
206+
207+
it('(obj)', function () {
208+
return collection.find('name', 'taylor').inc({
209+
qqqq: 1
210+
}).then(function () {
211+
return collection.findOne('name', 'taylor')
212+
}).then(function (doc) {
213+
assert.equal(8, doc.qqqq)
214+
})
215+
})
193216
})
194217

195218
describe('.find().unset()', function () {

0 commit comments

Comments
 (0)