summaryrefslogtreecommitdiff
path: root/jstests/core/drop_collection.js
blob: 2d44ca0379ff5de9de3effcbf2c76e25e7ce550a (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
/**
 * Basic test for the drop collection command
 *
 * @tags: [
 *   # Cannot implicitly shard accessed collections because of collection
 *   # existing when none expected.
 *   assumes_no_implicit_collection_creation_after_drop,
 *   no_selinux,
 * ]
 */

(function() {
"use strict";

function assertCollectionDropped(ns) {
    const coll = db[ns];
    // No more coll entry
    assert.eq(null, coll.exists(), "Collection exists after being dropped.");
    // No more documents
    assert.eq(0, coll.countDocuments({}), "Found leftover documents for dropped collection.");
    // No more indexes
    assert.eq(0, coll.getIndexes().length, "Found leftover indexes for dropped collection.");
}

const coll = db[jsTestName() + "_coll"];

const isMongos = db.adminCommand({isdbgrid: 1}).isdbgrid;

jsTest.log("Drop Unexistent collection.");
{
    // Drop the collection
    let dropRes = db.runCommand({drop: coll.getName()});
    if (isMongos) {
        assert.commandWorked(dropRes);
    } else {
        // TODO SERVER-43894 Make dropping a nonexistent collection a noop
        assert.commandWorkedOrFailedWithCode(dropRes, ErrorCodes.NamespaceNotFound);
    }
    assertCollectionDropped(coll.getFullName());
}

jsTest.log("Drop existing collection.");
{
    // Create the collection
    assert.commandWorked(coll.insert({x: 1}));
    assert.eq(1, coll.countDocuments({x: 1}));
    assert.eq(1, coll.getIndexes().length);
    // Drop the collection
    assert.commandWorked(db.runCommand({drop: coll.getName()}));
    assertCollectionDropped(coll.getFullName());

    // Test idempotency
    let dropRes = db.runCommand({drop: coll.getName()});
    if (isMongos) {
        assert.commandWorked(dropRes);
    } else {
        // TODO SERVER-43894 Make dropping a nonexistent collection a noop
        assert.commandWorkedOrFailedWithCode(dropRes, ErrorCodes.NamespaceNotFound);
    }
    assertCollectionDropped(coll.getFullName());
}

jsTest.log("Drop collection with multiple indexes.");
{
    assert.commandWorked(coll.insert({x: 1}));
    assert.eq(1, coll.countDocuments({x: 1}));
    coll.createIndex({a: 1});
    assert.eq(2, coll.getIndexes().length);
    assert.commandWorked(db.runCommand({dropIndexes: coll.getName(), index: "*"}));
    assert.eq(1, coll.getIndexes().length);
    // Drop the collection
    assert.commandWorked(db.runCommand({drop: coll.getName()}));
    assertCollectionDropped(coll.getFullName());
}
})();