summaryrefslogtreecommitdiff
path: root/jstests/core/nestedarr1.js
blob: 7edef9db512d858bd02bf038d72996b2f2480289 (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
// @tags: [requires_getmore, requires_fastcount]

/**
 * Inserts documents with an indexed nested array field, progressively increasing the nesting
 * depth until the key is too large to index. This tests that we support at least the minimum
 * supported BSON nesting depth, as well as maintaining index consistency.
 */
(function() {
"use strict";

function makeNestArr(depth) {
    if (depth == 1) {
        return {a: 1};
    } else if (depth == 2) {
        return {a: [1]};
    } else {
        return {a: [makeNestArr(depth - 2)]};
    }
}

let collection = db.arrNestTest;
collection.drop();

assert.commandWorked(collection.ensureIndex({a: 1}));

const kMaxDocumentDepthSoftLimit = 100;
const kJavaScriptMaxDepthLimit = 150;

let level;
for (level = 1; level < kJavaScriptMaxDepthLimit - 3; level++) {
    let res = db.runCommand({insert: collection.getName(), documents: [makeNestArr(level)]});
    if (!res.ok) {
        assert.commandFailedWithCode(
            res, 17280, "Expected insertion to fail only because key is too large to index");
        break;
    }
}

assert.gt(level,
          kMaxDocumentDepthSoftLimit,
          "Unable to insert a document nested with " + level +
              " levels, which is less than the supported limit of " + kMaxDocumentDepthSoftLimit);
assert.eq(collection.count(),
          collection.find().hint({a: 1}).itcount(),
          "Number of documents in collection does not match number of entries in index");
}());