summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/out/replace_collection.js
blob: febd3b541375d3c0bcab398f08b8ad8f055c9040 (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
/**
 * Tests the behavior of legacy $out.
 *
 * This test assumes that collections are not implicitly sharded, since $out is prohibited if the
 * output collection is sharded.
 */
(function() {
"use strict";

load("jstests/aggregation/extras/merge_helpers.js");  // For dropWithoutImplicitRecreate.
load("jstests/aggregation/extras/utils.js");          // For assertErrorCode.
load("jstests/libs/fixture_helpers.js");              // For FixtureHelpers.isMongos.

const coll = db.source;
const targetCollName = "target";
coll.drop();

dropWithoutImplicitRecreate(targetCollName);
const pipeline = [{$out: targetCollName}];

//
// Test $out with a non-existent output collection.
//
assert.commandWorked(coll.insert({_id: 0}));
coll.aggregate(pipeline);
const targetColl = db.target;
assert.eq(1, targetColl.find().itcount());

// Test $out with a non-existent database. This is only expected to work in a
// non-sharded environment.
const destDB = db.getSiblingDB("outDifferentDB");
destDB.dropDatabase();
if (FixtureHelpers.isMongos(db)) {
    assert.commandFailedWithCode(db.runCommand({
        aggregate: coll.getName(),
        cursor: {},
        pipeline: [{$out: {db: destDB.getName(), coll: 'outDifferentColl'}}]
    }),
                                 ErrorCodes.NamespaceNotFound);
} else {
    coll.aggregate({$out: {db: destDB.getName(), coll: destDB.outDifferentColl.getName()}});
    assert.eq(1, destDB.outDifferentColl.find().itcount());
}

//
// Test $out with an existing output collection.
//
coll.aggregate(pipeline);
assert.eq(1, targetColl.find().itcount());

//
// Test that $out will preserve the indexes and options of the output collection.
//
dropWithoutImplicitRecreate(targetCollName);
assert.commandWorked(db.createCollection(targetColl.getName(), {validator: {a: {$gt: 0}}}));
assert.commandWorked(targetColl.createIndex({a: 1}));

coll.drop();
assert.commandWorked(coll.insert({a: 1}));

coll.aggregate(pipeline);
assert.eq(1, targetColl.find().itcount());
assert.eq(2, targetColl.getIndexes().length);

const listColl = db.runCommand({listCollections: 1, filter: {name: targetColl.getName()}});
assert.commandWorked(listColl);
assert.eq({a: {$gt: 0}}, listColl.cursor.firstBatch[0].options["validator"]);

//
// Test that $out fails if it violates a unique index constraint.
//
coll.drop();
assert.commandWorked(coll.insert([{_id: 0, a: 0}, {_id: 1, a: 0}]));
dropWithoutImplicitRecreate(targetCollName);
assert.commandWorked(targetColl.createIndex({a: 1}, {unique: true}));

assertErrorCode(coll, pipeline, ErrorCodes.DuplicateKey);

// Rerun a similar test, except populate the target collection with a document that conflics
// with one out of the pipeline. In this case, there is no unique key violation since the target
// collection will be dropped before renaming the source collection.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 0}));
targetColl.remove({});
assert.commandWorked(targetColl.insert({_id: 1, a: 0}));

coll.aggregate(pipeline);
assert.eq(1, targetColl.find().itcount());
assert.eq(2, targetColl.getIndexes().length);
}());