This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
copyCollection.js
112 lines (99 loc) · 2.92 KB
/
copyCollection.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copy one collection to another
*
* call as `mongo db template.js thisfile.js`
*
* See http://www.mongodb.org/display/DOCS/Developer+FAQ#DeveloperFAQ-HowdoIcopyallobjectsfromonedatabasecollectiontoanother
* Allegedly, this is the only way to do it in mongodb. Implemented as a batch script
*
* This is a somewhat accademic script - don't use it. if you need to copy one collection to another the fastest way
* is to do:
* mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop
*
* By faster here are some numbers from copying a random collection with a million rows (2GB of data):
* running this batch, step size 100, individual inserts, duration: 1147s
* running this batch, step size 100, batch inserts, duration: 1149s
* running a one line db.source.find().forEach(function(x) { db.target.insert(x); }): 1080s
* using mongo export piped to mongoimport: 300s
*
* However, it demonstrates how to write a script which optionally buffers db activity and "commits" once per slice
* instead of one row at a time.
*
* Define the (source) collection to copy, the (target) to collection - and run
* Can either be run a row at a time - or using batchInserts
*
*/
var options = {
to: 'target',
collection: 'items',
fields: {},
batchInserts: false
};
CopyCollection = new Batch(options, false);
CopyCollection.name = 'CopyCollection';
/**
* process
*
* if we're in batchInsert mode - buffer the found row to the stack property
* else, attempt to insert into the target collection
*
* @return void.
*/
CopyCollection.process = function() {
this.out('processing ' + this.currentRow._id, 4);
if (this.options.batchInserts) {
this.stack.push(this.currentRow);
} else {
try {
db[this.options.to].insert(this.currentRow);
} catch (err) {
out(err.message, 1);
}
}
};
/**
* start
*
* Run the standard start function, then drop the destination collection
*
* @return bool.
*/
CopyCollection.originalStart = CopyCollection.start;
CopyCollection.start = function() {
if (!this.originalStart()) {
return false;
}
if (this.total) {
this.out('Dropping ' + this.options.to + ' collection', 1);
try {
db[this.options.to].drop();
} catch (err) {
out(err.message, 1);
}
this.stack = [];
}
return true;
};
/**
* afterCursor
*
* IF we're in batchInsert mode - insert the buffered rows
* Then call the original afterCursor method
*
* @param count $count.
* @return bool.
*/
CopyCollection.originalAfterCursor = CopyCollection.afterCursor;
CopyCollection.afterCursor = function afterCursor(count) {
if (this.options.batchInserts) {
this.out('Bulk inserting ' + this.stack.length + ' rows into ' + this.options.to, 4);
try {
db[this.options.to].insert(this.stack);
} catch (err) {
out(err.message, 1);
}
this.stack = [];
}
return this.originalAfterCursor(count);
};
CopyCollection.run();