summaryrefslogtreecommitdiff
path: root/jstests/core/bulk_api_ordered.js
blob: 4efdd7c319b129aafd29ad5698f4374df81129f4 (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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]

var collectionName = "bulk_api_ordered";
var coll = db.getCollection(collectionName);
coll.drop();

var request;
var result;

jsTest.log("Starting bulk api ordered tests...");

/********************************************************
 *
 * Unordered tests should return same results for write command as
 * well as for the legacy operations
 *
 *******************************************************/
var executeTests = function() {
    /**
     * find() requires selector and $key is disallowed as field name
     */
    var bulkOp = coll.initializeOrderedBulkOp();

    assert.throws(function() {
        bulkOp.find();
    });
    assert.throws(function() {
        bulkOp.insert({$key: 1});
    });

    /**
     * Single successful ordered bulk operation
     */
    var bulkOp = coll.initializeOrderedBulkOp();
    bulkOp.insert({a: 1});
    bulkOp.find({a: 1}).updateOne({$set: {b: 1}});
    // no-op, should increment nMatched but not nModified
    bulkOp.find({a: 1}).updateOne({$set: {b: 1}});
    bulkOp.find({a: 2}).upsert().updateOne({$set: {b: 2}});
    bulkOp.insert({a: 3});
    bulkOp.find({a: 3}).update({$set: {b: 1}});
    bulkOp.find({a: 3}).upsert().update({$set: {b: 2}});
    bulkOp.find({a: 10}).upsert().update({$set: {b: 2}});
    bulkOp.find({a: 2}).replaceOne({a: 11});
    bulkOp.find({a: 11}).removeOne();
    bulkOp.find({a: 3}).remove({a: 3});
    var result = bulkOp.execute();
    assert.eq(2, result.nInserted);
    assert.eq(2, result.nUpserted);
    assert.eq(5, result.nMatched);
    // only check nModified if write commands are enabled
    if (coll.getMongo().writeMode() == "commands") {
        assert.eq(4, result.nModified);
    }
    assert.eq(2, result.nRemoved);
    var upserts = result.getUpsertedIds();
    assert.eq(2, upserts.length);
    assert.eq(3, upserts[0].index);
    assert(upserts[0]._id != null);
    var upsert = result.getUpsertedIdAt(0);
    assert.eq(3, upsert.index);
    assert(upsert._id != null);
    assert.eq(2, coll.find({}).itcount(), "find should return two documents");

    // illegal to try to convert a multi-op batch into a SingleWriteResult
    assert.throws(function() {
        result.toSingleResult();
    });

    // attempt to re-run bulk operation
    assert.throws(function() {
        bulkOp.execute();
    });

    // Test SingleWriteResult
    var singleBatch = coll.initializeOrderedBulkOp();
    singleBatch.find({a: 4}).upsert().updateOne({$set: {b: 1}});
    var singleResult = singleBatch.execute().toSingleResult();
    assert(singleResult.getUpsertedId() != null);

    // Create unique index
    coll.remove({});
    coll.ensureIndex({a: 1}, {unique: true});

    /**
     * Single error ordered bulk operation
     */
    var bulkOp = coll.initializeOrderedBulkOp();
    bulkOp.insert({b: 1, a: 1});
    bulkOp.find({b: 2}).upsert().updateOne({$set: {a: 1}});
    bulkOp.insert({b: 3, a: 2});
    var result = assert.throws(function() {
        bulkOp.execute();
    });
    assert(result instanceof BulkWriteError);
    assert(result instanceof Error);
    // Basic properties check
    assert.eq(1, result.nInserted);
    assert.eq(true, result.hasWriteErrors());
    assert.eq(1, result.getWriteErrorCount());

    // Get the first error
    var error = result.getWriteErrorAt(0);
    assert.eq(11000, error.code);
    assert(error.errmsg != null);

    // Get the operation that caused the error
    var op = error.getOperation();
    assert.eq(2, op.q.b);
    assert.eq(1, op.u['$set'].a);
    assert.eq(false, op.multi);
    assert.eq(true, op.upsert);

    // Get the first error
    var error = result.getWriteErrorAt(1);
    assert.eq(null, error);

    // Create unique index
    coll.dropIndexes();
    coll.remove({});
    coll.ensureIndex({a: 1}, {unique: true});

    /**
     * Multiple error ordered bulk operation
     */
    var bulkOp = coll.initializeOrderedBulkOp();
    bulkOp.insert({b: 1, a: 1});
    bulkOp.find({b: 2}).upsert().updateOne({$set: {a: 1}});
    bulkOp.find({b: 3}).upsert().updateOne({$set: {a: 2}});
    bulkOp.find({b: 2}).upsert().updateOne({$set: {a: 1}});
    bulkOp.insert({b: 4, a: 3});
    bulkOp.insert({b: 5, a: 1});
    var result = assert.throws(function() {
        bulkOp.execute();
    });

    // Basic properties check
    assert.eq(1, result.nInserted);
    assert.eq(true, result.hasWriteErrors());
    assert.eq(1, result.getWriteErrorCount());

    // Individual error checking
    var error = result.getWriteErrorAt(0);
    assert.eq(1, error.index);
    assert.eq(11000, error.code);
    assert(error.errmsg != null);
    assert.eq(2, error.getOperation().q.b);
    assert.eq(1, error.getOperation().u['$set'].a);
    assert.eq(false, error.getOperation().multi);
    assert.eq(true, error.getOperation().upsert);

    // Create unique index
    coll.dropIndexes();
    coll.remove({});
    coll.ensureIndex({a: 1}, {unique: true});
};

var buildVersion = parseInt(db.runCommand({buildInfo: 1}).versionArray.slice(0, 3).join(""), 10);
// Save the existing useWriteCommands function
var _useWriteCommands = coll.getMongo().useWriteCommands;

//
// Only execute write command tests if we have > 2.5.5 otherwise
// execute the down converted version
if (buildVersion >= 255) {
    // Force the use of useWriteCommands
    coll._mongo.useWriteCommands = function() {
        return true;
    };

    // Execute tests using legacy operations
    executeTests();
}

// Force the use of legacy commands
coll._mongo.useWriteCommands = function() {
    return false;
};

// Execute tests using legacy operations
executeTests();

// Reset the function
coll.getMongo().useWriteCommands = _useWriteCommands;