summaryrefslogtreecommitdiff
path: root/jstests/sharding/json_schema.js
blob: b2a1ff21c777119aec6a11b60d3085a082a49f19 (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
58
59
60
61
62
63
64
65
/**
 * Tests for $jsonSchema queries in a sharded cluster.
 */
(function() {
"use strict";

const dbName = "json_schema_sharding";

var st = new ShardingTest({shards: 2, mongos: 1, config: 1});

assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.name);

const testDB = st.s.getDB(dbName);
const coll = testDB.json_schema_sharding;
coll.drop();

// Shard the collection on _id.
assert.commandWorked(testDB.adminCommand({shardCollection: coll.getFullName(), key: {_id: 1}}));

// Split the collection into 4 chunks: [MinKey, -100), [-100, 0), [0, 100), [100, MaxKey).
assert.commandWorked(testDB.adminCommand({split: coll.getFullName(), middle: {_id: -100}}));
assert.commandWorked(testDB.adminCommand({split: coll.getFullName(), middle: {_id: 0}}));
assert.commandWorked(testDB.adminCommand({split: coll.getFullName(), middle: {_id: 100}}));

// Move the [0, 100) and [100, MaxKey) chunks to st.shard1.shardName.
assert.commandWorked(
    testDB.adminCommand({moveChunk: coll.getFullName(), find: {_id: 50}, to: st.shard1.shardName}));
assert.commandWorked(testDB.adminCommand(
    {moveChunk: coll.getFullName(), find: {_id: 150}, to: st.shard1.shardName}));

// Write one document into each of the chunks.
assert.commandWorked(coll.insert({_id: -150, a: 1}));
assert.commandWorked(coll.insert({_id: -50, a: 10}));
assert.commandWorked(coll.insert({_id: 50, a: "str"}));
assert.commandWorked(coll.insert({_id: 150}));

// Test that $jsonSchema in a find command returns the correct results.
assert.eq(4, coll.find({$jsonSchema: {}}).itcount());
assert.eq(3, coll.find({$jsonSchema: {properties: {a: {type: "number"}}}}).itcount());
assert.eq(4, coll.find({$jsonSchema: {required: ["_id"]}}).itcount());
assert.eq(1, coll.find({$jsonSchema: {properties: {_id: {minimum: 150}}}}).itcount());

// Test that $jsonSchema works correctly in an update command.
let res = coll.update(
    {$jsonSchema: {properties: {_id: {type: "number", minimum: 100}, a: {type: "number"}}}},
    {$inc: {a: 1}},
    {multi: true});
assert.commandWorked(res);
assert.eq(1, res.nModified);

const schema = {
    properties: {_id: {type: "number", minimum: 100}},
    required: ["_id"]
};
res = coll.update({$jsonSchema: schema}, {$set: {b: 1}}, {multi: true});
assert.commandWorked(res);
assert.eq(1, res.nModified);

// Test that $jsonSchema works correctly in a findAndModify command.
res = coll.findAndModify({query: {_id: 150, $jsonSchema: schema}, update: {$set: {b: 1}}});
assert.eq(1, res.b);

st.stop();
})();