summaryrefslogtreecommitdiff
path: root/jstests/core/find_and_modify_empty_update.js
blob: a9b8cb55b34e907cf75f3c4abea781f831637c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Cannot implicitly shard accessed collections because of following errmsg: A single
// update/delete on a sharded collection must contain an exact match on _id or contain the shard
// key.
// @tags: [assumes_unsharded_collection]

// Test passing update:{} to findAndModify.  SERVER-13883.

var coll = db.find_and_modify_empty_update;
var ret;
coll.drop();
coll.getDB().createCollection(coll.getName());

// Test update:{} when no documents match the query.
ret = coll.findAndModify({query: {a: 1}, update: {}});
assert.isnull(ret);

// Test update:{} when a document matches the query.  The document is "replaced with the empty
// object" (i.e. all non-_id fields are unset).
coll.remove({});
assert.writeOK(coll.insert({_id: 0, a: 1}));
ret = coll.findAndModify({query: {a: 1}, update: {}});
assert.eq(ret, {_id: 0, a: 1});
assert.eq(coll.findOne({_id: 0}), {_id: 0});

// Test update:{} with new:true.
coll.remove({});
assert.writeOK(coll.insert({_id: 0, a: 1}));
ret = coll.findAndModify({query: {a: 1}, update: {}, new: true});
assert.eq(ret, {_id: 0});
assert.eq(coll.findOne({_id: 0}), {_id: 0});

// Test update:{} with a sort.
coll.remove({});
assert.writeOK(coll.insert({_id: 0, a: 1}));
assert.writeOK(coll.insert({_id: 1, a: 1}));
ret = coll.findAndModify({query: {a: 1}, update: {}, sort: {_id: 1}});
assert.eq(ret, {_id: 0, a: 1});
assert.eq(coll.findOne({_id: 0}), {_id: 0});

// Test update:{} with upsert:true.
coll.remove({});
ret = coll.findAndModify({query: {_id: 0, a: 1}, update: {}, upsert: true});
assert.isnull(ret);
assert.eq(coll.findOne({_id: 0}), {_id: 0});

// Test update:{} with a sort and upsert:true.
coll.remove({});
ret = coll.findAndModify({query: {_id: 0, a: 1}, update: {}, upsert: true, sort: {a: 1}});
assert.isnull(ret);
assert.eq(coll.findOne({_id: 0}), {_id: 0});

// Test update:{} with a sort, upsert:true, and new:true.
coll.remove({});
ret =
    coll.findAndModify({query: {_id: 0, a: 1}, update: {}, upsert: true, sort: {a: 1}, new: true});
assert.eq(ret, {_id: 0});
assert.eq(coll.findOne({_id: 0}), {_id: 0});