forked from ulpian/bson2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbson2csv.js
63 lines (55 loc) · 1.7 KB
/
bson2csv.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
// Generated by CoffeeScript 1.6.3
(function() {
var BSON, bchild, bson_file, bson_file_path, buffer, csv_file_path, exec, fs, jsoncsv, mongo,
_this = this;
require('coffee-script');
exec = require('child_process').exec;
fs = require('fs');
mongo = require('mongodb');
BSON = new mongo.BSON();
jsoncsv = require('json2csv');
if (process.argv[2] === '--bf') {
bson_file_path = process.argv[3];
csv_file_path = process.argv[5];
buffer = null;
this.jobj = [];
bson_file = fs.readFileSync(bson_file_path);
bchild = exec('bsondump ' + bson_file_path, function(err, stdout, stderr) {
var fields, object;
if (err) {
console.log('Error: ' + err);
}
object = stdout;
object = object.replace(/ObjectId\(/g, '');
object = object.replace(/\ \)/g, '');
object = object.replace(/Date\( /g, '');
object = object.replace(/\}\n\{/g, '},{');
object = object.replace(/\n/g, '');
object = object.replace(/\'\{/g, '{');
object = object.replace(/\}\'/g, '}');
object = '[' + object + ']';
_this.jobj = JSON.parse(object);
fields = Object.keys(_this.jobj[0]);
jsoncsv({
data: _this.jobj,
fields: fields
}, function(err, csv) {
if (err) {
console.log(err);
}
return fs.writeFile(csv_file_path, csv, function(err) {
if (err) {
return console.log(err);
}
});
});
if (process.argv[6] === '--log') {
if (process.argv[7] === 'true') {
return console.log(_this.jobj);
}
}
});
} else {
console.log('Error: You must define -bf (the bson file path)');
}
}).call(this);