summaryrefslogtreecommitdiff
path: root/jstests/core/max_doc_size.js
blob: d47c8eebc5fb976db7f075de4555e7bec21884e4 (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
66
67
68
69
70
71
72
// The {$set: {x: maxStr}}} update takes multiple seconds to execute.
// @tags: [operations_longer_than_stepdown_interval]

/**
 * Confirms that:
 *  - Documents at the maximum BSON size limit can be written and read back.
 *  - Documents over the maximum BSON size limit cannot be written.
 */
(function() {
'use strict';

const maxBsonObjectSize = db.hello().maxBsonObjectSize;
const docOverhead = Object.bsonsize({_id: new ObjectId(), x: ''});
const maxStrSize = maxBsonObjectSize - docOverhead;
const maxStr = 'a'.repeat(maxStrSize);
const coll = db.max_doc_size;

//
// Test that documents at the size limit can be written and read back.
//
coll.drop();
assert.commandWorked(
    db.runCommand({insert: coll.getName(), documents: [{_id: new ObjectId(), x: maxStr}]}));
assert.eq(coll.find({}).itcount(), 1);

coll.drop();
const objectId = new ObjectId();
assert.commandWorked(db.runCommand({
    update: coll.getName(),
    ordered: true,
    updates: [{q: {_id: objectId}, u: {_id: objectId, x: maxStr}, upsert: true}]
}));
assert.eq(coll.find({}).itcount(), 1);

coll.drop();

assert.commandWorked(coll.insert({_id: objectId}));
assert.commandWorked(db.runCommand({
    update: coll.getName(),
    ordered: true,
    updates: [{q: {_id: objectId}, u: {$set: {x: maxStr}}}]
}));
assert.eq(coll.find({}).itcount(), 1);

//
// Test that documents over the size limit cannot be written.
//
const largerThanMaxString = maxStr + 'a';

coll.drop();
assert.commandFailedWithCode(
    db.runCommand(
        {insert: coll.getName(), documents: [{_id: new ObjectId(), x: largerThanMaxString}]}),
    2);

coll.drop();
assert.commandFailedWithCode(db.runCommand({
    update: coll.getName(),
    ordered: true,
    updates: [{q: {_id: objectId}, u: {_id: objectId, x: largerThanMaxString}, upsert: true}]
}),
                             17420);

coll.drop();
assert.commandWorked(coll.insert({_id: objectId}));
assert.commandFailedWithCode(db.runCommand({
    update: coll.getName(),
    ordered: true,
    updates: [{q: {_id: objectId}, u: {$set: {x: largerThanMaxString}}}]
}),
                             17419);
})();