summaryrefslogtreecommitdiff
path: root/jstests/core/profile_findandmodify.js
blob: 42a8be46fe259de4c50490cf7f3edc6461b47306 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
// Confirms that profiled findAndModify execution contains all expected metrics with proper values.
// @tags: [
//   # Asserts on the number of index keys modified.
//   assumes_no_implicit_index_creation,
//   requires_profiling,
// ]

(function() {
"use strict";

load("jstests/libs/clustered_collections/clustered_collection_util.js");
load("jstests/libs/profiler.js");  // For getLatestProfilerEntry.

var testDB = db.getSiblingDB("profile_findandmodify");
assert.commandWorked(testDB.dropDatabase());
var coll = testDB.getCollection("test");

assert.commandWorked(testDB.setProfilingLevel(2));

//
// Update as findAndModify.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i, b: [0]}));
}
assert.commandWorked(coll.createIndex({b: 1}));

assert.eq({_id: 2, a: 2, b: [0]}, coll.findAndModify({
    query: {a: 2},
    update: {$inc: {"b.$[i]": 1}},
    collation: {locale: "fr"},
    arrayFilters: [{i: 0}]
}));

var profileObj = getLatestProfilerEntry(testDB);

assert.eq(profileObj.op, "command", tojson(profileObj));
assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.command.query, {a: 2}, tojson(profileObj));
assert.eq(profileObj.command.update, {$inc: {"b.$[i]": 1}}, tojson(profileObj));
assert.eq(profileObj.command.collation, {locale: "fr"}, tojson(profileObj));
assert.eq(profileObj.command.arrayFilters, [{i: 0}], tojson(profileObj));
assert.eq(profileObj.keysExamined, 0, tojson(profileObj));
assert.eq(profileObj.docsExamined, 3, tojson(profileObj));
assert.eq(profileObj.nMatched, 1, tojson(profileObj));
assert.eq(profileObj.nModified, 1, tojson(profileObj));
assert.eq(profileObj.keysInserted, 1, tojson(profileObj));
assert.eq(profileObj.keysDeleted, 1, tojson(profileObj));
assert.eq(profileObj.planSummary, "COLLSCAN", tojson(profileObj));
assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
assert(profileObj.hasOwnProperty("numYield"), tojson(profileObj));
assert(profileObj.hasOwnProperty("responseLength"), tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Delete as findAndModify.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

const collectionIsClustered = ClusteredCollectionUtil.areAllCollectionsClustered(db.getMongo());
// A clustered collection doesn't actually have an index on _id.
const expectedKeysDeleted = collectionIsClustered ? 0 : 1;

assert.eq({_id: 2, a: 2}, coll.findAndModify({query: {a: 2}, remove: true}));
profileObj = getLatestProfilerEntry(testDB);
assert.eq(profileObj.op, "command", tojson(profileObj));
assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.command.query, {a: 2}, tojson(profileObj));
assert.eq(profileObj.command.remove, true, tojson(profileObj));
assert.eq(profileObj.keysExamined, 0, tojson(profileObj));
assert.eq(profileObj.docsExamined, 3, tojson(profileObj));
assert.eq(profileObj.ndeleted, 1, tojson(profileObj));
assert.eq(profileObj.keysDeleted, expectedKeysDeleted, tojson(profileObj));
assert.eq(profileObj.planSummary, "COLLSCAN", tojson(profileObj));
assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Update with {upsert: true} as findAndModify.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

// A clustered collection doesn't actually have an index on _id, and queries involve an efficient
// bounded collection scan.
const expectedKeysInserted = collectionIsClustered ? 0 : 1;
const expectedDocsExamined = collectionIsClustered ? 1 : 0;
assert.eq({_id: 4, a: 1},
          coll.findAndModify({query: {_id: 4}, update: {$inc: {a: 1}}, upsert: true, new: true}));
profileObj = getLatestProfilerEntry(testDB);
assert.eq(profileObj.op, "command", tojson(profileObj));
assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.command.query, {_id: 4}, tojson(profileObj));
assert.eq(profileObj.command.update, {$inc: {a: 1}}, tojson(profileObj));
assert.eq(profileObj.command.upsert, true, tojson(profileObj));
assert.eq(profileObj.command.new, true, tojson(profileObj));
assert.eq(profileObj.keysExamined, 0, tojson(profileObj));
assert.eq(profileObj.docsExamined, expectedDocsExamined, tojson(profileObj));
assert.eq(profileObj.nMatched, 0, tojson(profileObj));
assert.eq(profileObj.nModified, 0, tojson(profileObj));
assert.eq(profileObj.nUpserted, 1, tojson(profileObj));
assert.eq(profileObj.keysInserted, expectedKeysInserted, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Idhack update as findAndModify.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

const expectedKeysExamined = collectionIsClustered ? 0 : 1;
const expectedPlan = collectionIsClustered ? "CLUSTERED_IXSCAN" : "IDHACK";

assert.eq({_id: 2, a: 2}, coll.findAndModify({query: {_id: 2}, update: {$inc: {b: 1}}}));
profileObj = getLatestProfilerEntry(testDB);
assert.eq(profileObj.keysExamined, expectedKeysExamined, tojson(profileObj));
assert.eq(profileObj.docsExamined, 1, tojson(profileObj));
assert.eq(profileObj.nMatched, 1, tojson(profileObj));
assert.eq(profileObj.nModified, 1, tojson(profileObj));
assert.eq(profileObj.planSummary, expectedPlan, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Update as findAndModify with projection.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

assert.eq({a: 2},
          coll.findAndModify({query: {a: 2}, update: {$inc: {b: 1}}, fields: {_id: 0, a: 1}}));
profileObj = getLatestProfilerEntry(testDB);
assert.eq(profileObj.op, "command", tojson(profileObj));
assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.command.query, {a: 2}, tojson(profileObj));
assert.eq(profileObj.command.update, {$inc: {b: 1}}, tojson(profileObj));
assert.eq(profileObj.command.fields, {_id: 0, a: 1}, tojson(profileObj));
assert.eq(profileObj.keysExamined, 0, tojson(profileObj));
assert.eq(profileObj.docsExamined, 3, tojson(profileObj));
assert.eq(profileObj.nMatched, 1, tojson(profileObj));
assert.eq(profileObj.nModified, 1, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Delete as findAndModify with projection.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

assert.eq({a: 2}, coll.findAndModify({query: {a: 2}, remove: true, fields: {_id: 0, a: 1}}));
profileObj = getLatestProfilerEntry(testDB);
assert.eq(profileObj.op, "command", tojson(profileObj));
assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.command.query, {a: 2}, tojson(profileObj));
assert.eq(profileObj.command.remove, true, tojson(profileObj));
assert.eq(profileObj.command.fields, {_id: 0, a: 1}, tojson(profileObj));
assert.eq(profileObj.ndeleted, 1, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Confirm "hasSortStage" on findAndModify with sort.
//
coll.drop();
for (var i = 0; i < 3; i++) {
    assert.commandWorked(coll.insert({_id: i, a: i}));
}

assert.eq({_id: 0, a: 0},
          coll.findAndModify({query: {a: {$gte: 0}}, sort: {a: 1}, update: {$inc: {b: 1}}}));

profileObj = getLatestProfilerEntry(testDB);

assert.eq(profileObj.hasSortStage, true, tojson(profileObj));

//
// Confirm "fromMultiPlanner" metric.
//
coll.drop();
assert.commandWorked(coll.createIndex({a: 1}));
assert.commandWorked(coll.createIndex({b: 1}));
for (i = 0; i < 5; ++i) {
    assert.commandWorked(coll.insert({a: i, b: i}));
}

coll.findAndModify({query: {a: 3, b: 3}, update: {$set: {c: 1}}});
profileObj = getLatestProfilerEntry(testDB);

assert.eq(profileObj.fromMultiPlanner, true, tojson(profileObj));
})();