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
|
(function() {
if (typeof getToolTest === 'undefined') {
load('jstests/configs/plain_28.config.js');
}
// Tests that running mongorestore with --drop and --collection leaves data
// in other collections untouched (that --drop only applies to the
// specified collection).
jsTest.log('Testing restoration with --drop and --collection, with data in'+
' other collections');
var toolTest = getToolTest('drop_one_collection');
var commonToolArgs = getCommonToolArguments();
// where we'll put the dump
var dumpTarget = 'drop_one_collection_dump';
resetDbpath(dumpTarget);
// the db we will take the dump from
var sourceDB = toolTest.db.getSiblingDB('source');
// dump from two different collections, even though we'll
// only be restoring one.
var collNames = ['coll1', 'coll2'];
collNames.forEach(function(collName) {
for (var i = 0; i < 500; i++) {
sourceDB[collName].insert({_id: i+'_'+collName});
}
// sanity check the insertion worked
assert.eq(500, sourceDB[collName].count());
});
// dump the data
var ret = toolTest.runTool.apply(toolTest, ['dump']
.concat(getDumpTarget(dumpTarget))
.concat(commonToolArgs));
assert.eq(0, ret);
// drop and replace the data
collNames.forEach(function(collName) {
sourceDB[collName].drop();
// sanity check the drop worked
assert.eq(0, sourceDB[collName].count());
// insert a disjoint set of data from the dump
for (var i = 500; i < 600; i++) {
sourceDB[collName].insert({_id: i+'_'+collName});
}
// sanity check the insertion worked
assert.eq(100, sourceDB[collName].count());
});
// insert data into the same collections in a different db
var otherDB = toolTest.db.getSiblingDB('other');
collNames.forEach(function(collName) {
for (var i = 500; i < 600; i++) {
otherDB[collName].insert({_id: i+'_'+collName});
}
// sanity check the insertion worked
assert.eq(100, otherDB[collName].count());
});
// restore with --drop and --collection
ret = toolTest.runTool.apply(toolTest, ['restore', '--drop',
'--db', 'source',
'--collection', 'coll1']
.concat(getRestoreTarget(dumpTarget+'/source/coll1.bson'))
.concat(commonToolArgs));
assert.eq(0, ret);
// make sure that the dumped data replaced the old data in only
// the specified collection, and all other data was left untouched
assert.eq(500, sourceDB.coll1.count());
for (var i = 0; i < 500; i++) {
assert.eq(1, sourceDB.coll1.count({_id: i+'_coll1'}));
}
assert.eq(100, sourceDB.coll2.count());
assert.eq(100, otherDB.coll1.count());
assert.eq(100, otherDB.coll2.count());
// success
toolTest.stop();
}());
|