forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_collection_different_dbs.js
80 lines (64 loc) · 2.27 KB
/
rename_collection_different_dbs.js
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
/*
* Test renameCollection functionality across different databases.
*
* @tags: [
* # On sharded cluster primary shard for database can land in different
* # shards and rename across different primary shards is not allowed.
* assumes_against_mongod_not_mongos,
* assumes_no_implicit_index_creation,
* # Rename across DBs with different shard primary is not supported
* assumes_unsharded_collection,
* requires_capped,
* requires_collstats,
* requires_non_retryable_commands,
* uses_rename,
* ]
*/
// Set up namespaces a and b.
const db_a = db.getSiblingDB("db_a");
const db_b = db.getSiblingDB("db_b");
let a = db_a.rename_different_db;
let b = db_b.rename_different_db;
a.drop();
b.drop();
// Put some documents and indexes in a.
a.insertMany([{a: 1}, {a: 2}, {a: 3}]);
assert.commandWorked(a.createIndexes([{a: 1}, {b: 1}]));
assert.commandWorked(db.adminCommand(
{renameCollection: "db_a.rename_different_db", to: "db_b.rename_different_db"}));
assert.eq(0, a.countDocuments({}));
assert(db_a.getCollectionNames().indexOf(a.getName()) < 0);
assert.eq(3, b.countDocuments({}));
assert(db_b.getCollectionNames().indexOf(a.getName()) >= 0);
a.drop();
b.drop();
// Test that the dropTarget option works when renaming across databases.
a.save({});
b.save({});
assert.commandFailed(db.adminCommand(
{renameCollection: "db_a.rename_different_db", to: "db_b.rename_different_db"}));
assert.commandWorked(db.adminCommand({
renameCollection: "db_a.rename_different_db",
to: "db_b.rename_different_db",
dropTarget: true
}));
a.drop();
b.drop();
// Capped collection testing.
db_a.createCollection("rename_capped", {capped: true, size: 10000});
a = db_a.rename_capped;
b = db_b.rename_capped;
a.insertMany([{a: 1}, {a: 2}, {a: 3}]);
let previousMaxSize = assert.commandWorked(a.stats()).maxSize;
assert.commandWorked(
db.adminCommand({renameCollection: "db_a.rename_capped", to: "db_b.rename_capped"}));
assert.eq(0, a.countDocuments({}));
assert(db_a.getCollectionNames().indexOf(a.getName()) < 0);
assert.eq(3, b.countDocuments({}));
assert(db_b.getCollectionNames().indexOf(a.getName()) >= 0);
let stats = assert.commandWorked(db_b.rename_capped.stats());
printjson(stats);
assert(stats.capped);
assert.eq(previousMaxSize, stats.maxSize);
a.drop();
b.drop();