summaryrefslogtreecommitdiff
path: root/jstests/core/list_collections1.js
blob: 7526573be3ff5ff20f73982cc919f6b1d6cffa6a (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Basic functional tests for the listCollections command.
 *
 * @tags: [
 *   # Cannot implicitly shard accessed collections
 *   # because of collection existing when none expected.
 *   assumes_no_implicit_collection_creation_after_drop,
 *   # applyOps is not supported on mongos
 *   assumes_against_mongod_not_mongos,
 *   requires_getmore,
 *   requires_replication,
 *   uses_api_parameters,
 *   # Tenant migrations don't support applyOps.
 *   tenant_migration_incompatible,
 * ]
 *
 * Note that storage engines used to be allowed to advertise internal collections to the user (in
 * particular, the MMAPv1 storage engine used to advertise the "system.indexes" collection).
 * Hence, this test suite does not test for a particular number of collections returned in
 * listCollections output, but rather tests for existence or absence of particular collections in
 * listCollections output.
 */

(function() {
"use strict";

var mydb = db.getSiblingDB("list_collections1");
var cursor;
var res;
var collObj;

//
// Test basic command output.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
res = mydb.runCommand("listCollections");
assert.commandWorked(res);
assert.eq('object', typeof (res.cursor));
assert.eq(0, res.cursor.id);
assert.eq('string', typeof (res.cursor.ns));
collObj = res.cursor.firstBatch.filter(function(c) {
    return c.name === "foo";
})[0];
assert(collObj);
assert.eq('object', typeof (collObj.options));
assert.eq('collection', collObj.type, tojson(collObj));
assert.eq(false, collObj.info.readOnly, tojson(collObj));
assert.eq("object", typeof (collObj.idIndex), tojson(collObj));
assert(collObj.idIndex.hasOwnProperty("v"), tojson(collObj));

//
// Test basic command output for views.
//

assert.commandWorked(mydb.createView("bar", "foo", []));
res = mydb.runCommand("listCollections");
assert.commandWorked(res);
collObj = res.cursor.firstBatch.filter(function(c) {
    return c.name === "bar";
})[0];
assert(collObj);
assert.eq("object", typeof (collObj.options), tojson(collObj));
assert.eq("foo", collObj.options.viewOn, tojson(collObj));
assert.eq([], collObj.options.pipeline, tojson(collObj));
assert.eq("view", collObj.type, tojson(collObj));
assert.eq(true, collObj.info.readOnly, tojson(collObj));
assert(!collObj.hasOwnProperty("idIndex"), tojson(collObj));

//
// Test basic usage with DBCommandCursor.
//

var getListCollectionsCursor = function(options, subsequentBatchSize) {
    return new DBCommandCursor(
        mydb, mydb.runCommand("listCollections", options), subsequentBatchSize);
};

var cursorCountMatching = function(cursor, pred) {
    return cursor.toArray().filter(pred).length;
};

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.eq(1, cursorCountMatching(getListCollectionsCursor(), function(c) {
              return c.name === "foo";
          }));

//
// Test that the collection metadata object is returned correctly.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.commandWorked(mydb.runCommand(
    {applyOps: [{op: "c", ns: mydb.getName() + ".$cmd", o: {create: "bar", temp: true}}]}));
assert.eq(1, cursorCountMatching(getListCollectionsCursor(), function(c) {
              return c.name === "foo" && c.options.temp === undefined;
          }));
assert.eq(1, cursorCountMatching(getListCollectionsCursor(), function(c) {
              return c.name === "bar" && c.options.temp === true;
          }));

//
// Test basic usage of "filter" option.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.commandWorked(mydb.runCommand(
    {applyOps: [{op: "c", ns: mydb.getName() + ".$cmd", o: {create: "bar", temp: true}}]}));
assert.eq(2, cursorCountMatching(getListCollectionsCursor({filter: {}}), function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
assert.eq(2, getListCollectionsCursor({filter: {name: {$in: ["foo", "bar"]}}}).itcount());
assert.eq(1, getListCollectionsCursor({filter: {name: /^foo$/}}).itcount());
assert.eq(1, getListCollectionsCursor({filter: {"options.temp": true}}).itcount());
mydb.foo.drop();
assert.eq(1, cursorCountMatching(getListCollectionsCursor({filter: {}}), function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
assert.eq(1, getListCollectionsCursor({filter: {name: {$in: ["foo", "bar"]}}}).itcount());
assert.eq(0, getListCollectionsCursor({filter: {name: /^foo$/}}).itcount());
assert.eq(1, getListCollectionsCursor({filter: {"options.temp": true}}).itcount());
mydb.bar.drop();
assert.eq(0, cursorCountMatching(getListCollectionsCursor({filter: {}}), function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
assert.eq(0, getListCollectionsCursor({filter: {name: {$in: ["foo", "bar"]}}}).itcount());
assert.eq(0, getListCollectionsCursor({filter: {name: /^foo$/}}).itcount());
assert.eq(0, getListCollectionsCursor({filter: {"options.temp": true}}).itcount());

//
// Test for invalid values of "filter".
//

assert.throws(function() {
    getListCollectionsCursor({filter: {$invalid: 1}});
});
assert.throws(function() {
    getListCollectionsCursor({filter: 0});
});
assert.throws(function() {
    getListCollectionsCursor({filter: 'x'});
});
assert.throws(function() {
    getListCollectionsCursor({filter: []});
});

//
// Test basic usage of "cursor.batchSize" option.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.commandWorked(mydb.createCollection("bar"));
cursor = getListCollectionsCursor({cursor: {batchSize: 2}});
assert.eq(2, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
cursor = getListCollectionsCursor({cursor: {batchSize: 1}});
assert.eq(1, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
cursor = getListCollectionsCursor({cursor: {batchSize: 0}});
assert.eq(0, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));

cursor = getListCollectionsCursor({cursor: {batchSize: NumberInt(2)}});
assert.eq(2, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));
cursor = getListCollectionsCursor({cursor: {batchSize: NumberLong(2)}});
assert.eq(2, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));

// Test a large batch size, and assert that at least 2 results are returned in the initial
// batch.
cursor = getListCollectionsCursor({cursor: {batchSize: Math.pow(2, 62)}});
assert.lte(2, cursor.objsLeftInBatch());
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));

// Ensure that the server accepts an empty object for "cursor".  This is equivalent to not
// specifying "cursor" at all.
//
// We do not test for objsLeftInBatch() here, since the default batch size for this command
// is not specified.
cursor = getListCollectionsCursor({cursor: {}});
assert.eq(2, cursorCountMatching(cursor, function(c) {
              return c.name === "foo" || c.name === "bar";
          }));

//
// Test for invalid values of "cursor" and "cursor.batchSize".
//

assert.throws(function() {
    getListCollectionsCursor({cursor: 0});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: 'x'});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: []});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: {foo: 1}});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: {batchSize: -1}});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: {batchSize: 'x'}});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: {batchSize: {}}});
});
assert.throws(function() {
    getListCollectionsCursor({cursor: {batchSize: 2, foo: 1}});
});

//
// Test more than 2 batches of results.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.commandWorked(mydb.createCollection("bar"));
assert.commandWorked(mydb.createCollection("baz"));
assert.commandWorked(mydb.createCollection("quux"));
cursor = getListCollectionsCursor({cursor: {batchSize: 0}}, 2);
assert.eq(0, cursor.objsLeftInBatch());
assert(cursor.hasNext());
assert.eq(2, cursor.objsLeftInBatch());
cursor.next();
assert(cursor.hasNext());
assert.eq(1, cursor.objsLeftInBatch());
cursor.next();
assert(cursor.hasNext());
assert.eq(2, cursor.objsLeftInBatch());
cursor.next();
assert(cursor.hasNext());
assert.eq(1, cursor.objsLeftInBatch());

//
// Test that batches are limited to ~16 MB
//

assert.commandWorked(mydb.dropDatabase());
const validator = {
    $jsonSchema: {
        bsonType: "object",
        properties: {
            stringWith4mbDescription:
                {bsonType: "string", description: "x".repeat(3 * 1024 * 1024)},

        }
    }
};

// Each collection's info is about 3 MB; 4 collections fit in the first batch and 2 in the second.
const nCollections = 6;
jsTestLog(`Creating ${nCollections} collections with huge validator objects....`);
for (let i = 0; i < nCollections; i++) {
    assert.commandWorked(mydb.createCollection("collection_" + i, {validator: validator}));
}
jsTestLog(`Done creating ${nCollections} collections`);
cursor = getListCollectionsCursor();
assert(cursor.hasNext());
const firstBatchSize = cursor.objsLeftInBatch();
assert.gt(firstBatchSize, 0);
assert.lt(firstBatchSize, nCollections);
// Exhaust the first batch..
while (cursor.objsLeftInBatch()) {
    cursor.next();
}
assert(cursor.hasNext());
cursor.next();
assert.eq(firstBatchSize + cursor.objsLeftInBatch() + 1, nCollections);

//
// Test on non-existent database.
//

assert.commandWorked(mydb.dropDatabase());
cursor = getListCollectionsCursor();
assert.eq(0, cursorCountMatching(cursor, function(c) {
              return c.name === "foo";
          }));

//
// Test on empty database.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
mydb.foo.drop();
cursor = getListCollectionsCursor();
assert.eq(0, cursorCountMatching(cursor, function(c) {
              return c.name === "foo";
          }));

//
// Test killCursors against a listCollections cursor.
//

assert.commandWorked(mydb.dropDatabase());
assert.commandWorked(mydb.createCollection("foo"));
assert.commandWorked(mydb.createCollection("bar"));
assert.commandWorked(mydb.createCollection("baz"));
assert.commandWorked(mydb.createCollection("quux"));

res = mydb.runCommand("listCollections", {cursor: {batchSize: 0}});
cursor = new DBCommandCursor(mydb, res, 2);
cursor.close();
cursor = new DBCommandCursor(mydb, res, 2);
assert.throws(function() {
    cursor.hasNext();
});

//
// Test parsing of the 'includePendingDrops' flag. If included, its argument must be of
// 'boolean' type. Functional testing of the 'includePendingDrops' flag is done in
// "jstests/replsets".
//

// Bad argument types.
assert.commandFailedWithCode(mydb.runCommand("listCollections", {includePendingDrops: {}}),
                             ErrorCodes.TypeMismatch);
assert.commandFailedWithCode(mydb.runCommand("listCollections", {includePendingDrops: "s"}),
                             ErrorCodes.TypeMismatch);

// Valid argument types.
assert.commandWorked(mydb.runCommand("listCollections", {includePendingDrops: 1}));
assert.commandWorked(mydb.runCommand("listCollections", {includePendingDrops: true}));
assert.commandWorked(mydb.runCommand("listCollections", {includePendingDrops: false}));

// Verify that 'includePendingDrops' field is unstable in API version 1.
assert.commandFailedWithCode(
    mydb.runCommand("listCollections",
                    {includePendingDrops: false, apiVersion: "1", apiStrict: true}),
    ErrorCodes.APIStrictError);
}());