File: upsert_sharded.js

package info (click to toggle)
mongodb 1%3A3.2.11-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 196,404 kB
  • sloc: cpp: 1,412,793; ansic: 504,961; python: 78,522; perl: 5,837; sh: 5,661; java: 4,202; makefile: 1,784; pascal: 617; xml: 176; asm: 128
file content (106 lines) | stat: -rw-r--r-- 4,140 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Upsert behavior tests for sharding
// NOTE: Generic upsert behavior tests belong in the core suite
//

var options = {
    shardOptions: {verbose: 2}
};

var st = new ShardingTest({shards: 2, mongos: 1, other: options});
st.stopBalancer();

var mongos = st.s0;
var admin = mongos.getDB("admin");
var shards = mongos.getCollection("config.shards").find().toArray();
var coll = mongos.getCollection("foo.bar");

assert(admin.runCommand({enableSharding: coll.getDB() + ""}).ok);
st.ensurePrimaryShard(coll.getDB().getName(), 'shard0001');

var upsertedResult = function(query, expr) {
    coll.remove({});
    result = coll.update(query, expr, {upsert: true});
    return result;
};

var upsertedField = function(query, expr, fieldName) {
    assert.writeOK(upsertedResult(query, expr));
    return coll.findOne()[fieldName];
};

var upsertedId = function(query, expr) {
    return upsertedField(query, expr, "_id");
};

var upsertedXVal = function(query, expr) {
    return upsertedField(query, expr, "x");
};

printjson(admin.runCommand({movePrimary: coll.getDB() + "", to: shards[0]._id}));
assert(admin.runCommand({shardCollection: coll + "", key: {x: 1}}).ok);
assert(admin.runCommand({split: coll + "", middle: {x: 0}}).ok);
assert(admin.runCommand(
                 {moveChunk: coll + "", find: {x: 0}, to: shards[1]._id, _waitForDelete: true}).ok);

st.printShardingStatus();

// upserted update replacement would result in no shard key
assert.writeError(upsertedResult({x: 1}, {}));

// updates with upsert must contain shard key in query when $op style
assert.eq(1, upsertedXVal({x: 1}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal({x: {$eq: 1}}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal({x: {$all: [1]}}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal({$and: [{x: {$eq: 1}}]}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal({$or: [{x: {$eq: 1}}]}, {$set: {a: 1}}));

// shard key not extracted
assert.writeError(upsertedResult({}, {$set: {a: 1, x: 1}}));
assert.writeError(upsertedResult({x: {$gt: 1}}, {$set: {a: 1, x: 1}}));
assert.writeError(upsertedResult({x: {$in: [1]}}, {$set: {a: 1, x: 1}}));

// Shard key type errors
assert.writeError(upsertedResult({x: undefined}, {$set: {a: 1}}));
assert.writeError(upsertedResult({x: [1, 2]}, {$set: {a: 1}}));
assert.writeError(upsertedResult({x: {$eq: {$gt: 5}}}, {$set: {a: 1}}));
// Regex shard key is not extracted from queries, even exact matches
assert.writeError(upsertedResult({x: {$eq: /abc/}}, {$set: {a: 1}}));

// nested field extraction always fails with non-nested key - like _id, we require setting the
// elements directly
assert.writeError(upsertedResult({"x.x": 1}, {$set: {a: 1}}));
assert.writeError(upsertedResult({"x.x": {$eq: 1}}, {$set: {a: 1}}));

coll.drop();

printjson(admin.runCommand({movePrimary: coll.getDB() + "", to: shards[0]._id}));
assert(admin.runCommand({shardCollection: coll + "", key: {'x.x': 1}}).ok);
assert(admin.runCommand({split: coll + "", middle: {'x.x': 0}}).ok);
assert(admin.runCommand(
                 {moveChunk: coll + "", find: {'x.x': 0}, to: shards[1]._id, _waitForDelete: true})
           .ok);

st.printShardingStatus();

// nested field extraction with nested shard key
assert.docEq({x: 1}, upsertedXVal({"x.x": 1}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal({"x.x": {$eq: 1}}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal({"x.x": {$all: [1]}}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal({$and: [{"x.x": {$eq: 1}}]}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal({$or: [{"x.x": {$eq: 1}}]}, {$set: {a: 1}}));

// Can specify siblings of nested shard keys
assert.docEq({x: 1, y: 1}, upsertedXVal({"x.x": 1, "x.y": 1}, {$set: {a: 1}}));
assert.docEq({x: 1, y: {z: 1}}, upsertedXVal({"x.x": 1, "x.y.z": 1}, {$set: {a: 1}}));

// No arrays at any level
assert.writeError(upsertedResult({"x.x": []}, {$set: {a: 1}}));
assert.writeError(upsertedResult({x: {x: []}}, {$set: {a: 1}}));
assert.writeError(upsertedResult({x: [{x: 1}]}, {$set: {a: 1}}));

// Can't set sub-fields of nested key
assert.writeError(upsertedResult({"x.x.x": {$eq: 1}}, {$set: {a: 1}}));

jsTest.log("DONE!");
st.stop();