-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_shipping_points.js
122 lines (113 loc) · 4.08 KB
/
mongo_shipping_points.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
113
114
115
116
117
118
119
120
121
122
var MongoClient = require('mongodb').MongoClient;
var fs = require('fs');
var readline = require('linebyline');
var url = 'mongodb://localhost:27017/shalldon';
var items = [];
MongoClient.connect(url, function(err, db) {
var col_shipping = db.collection('shipping_points');
if (col_shipping) {
col_shipping.drop();
}
db.createCollection('shipping_points');
col_shipping = db.collection('shipping_points');
var lines = readline('./files/31JUL_shipping_points.csv');
lines.on('line', function(line, lineCount, byteCount) {
if (lineCount == 1) return;
if (lineCount % 1000 === 0) {
console.log(lineCount);
}
var arr = line.split(',');
var item = {};
if (arr) {
item.postcode = arr[0];
item.external_id = arr[1];
item.province = arr[2]
item.town = arr[3]
item.county = arr[4]
item.zipcode = arr[5]
if (arr[6].startsWith('"')) {
if (arr.length > 15) {
var i = arr.length - 1;
var addressArr = arr.slice(6, i - 7);
var addressResult = /\"?([\w\W]*)\"?/.exec(addressArr.join(''));
if (addressResult && addressResult.length > 1) {
item.address = addressResult[1];
} else {
item.address = "";
}
item.name = arr[i - 8];
item.mobile = arr[i - 7];
item.locations = (arr[i - 6] == "{}") ? null : arr[i - 6];
item.ndc = !(arr[i - 5] == 'f');
item.rdc = !(arr[i - 4] == 'f');
item.latitude = parseFloat(arr[i - 3]) ? parseFloat(arr[i - 3]) : null;
item.longitude = parseFloat(arr[i - 2]) ? parseFloat(arr[i - 2]) : null;
item.wh_id = arr[i - 1];
item.case = arr[i];
} else {
item.mobile = arr[8]
item.locations = (arr[9] == "{}") ? null : arr[9];
item.ndc = !(arr[10] == 'f');
item.rdc = !(arr[11] == 'f');
item.latitude = parseFloat(arr[12]) ? parseFloat(arr[12]) : null;
item.longitude = parseFloat(arr[13]) ? parseFloat(arr[13]) : null;
item.wh_id = arr[14];
item.case = arr[15];
}
} else if (arr[7].startsWith('"')) {
item.address = arr[6];
if (arr.length > 15) {
var i = arr.length - 1;
var nameArr = arr.slice(7, i - 7);
var nameResult = /\"?([\w\W]*)\"?/.exec(nameArr.join(','));
if (nameResult && nameResult.length > 1) {
item.name = nameResult[1];
} else {
item.name = "";
}
item.mobile = arr[i - 7];
item.locations = (arr[i - 6] == "{}") ? null : arr[i - 6];
item.ndc = !(arr[i - 5] == 'f');
item.rdc = !(arr[i - 4] == 'f');
item.latitude = parseFloat(arr[i - 3]) ? parseFloat(arr[i - 3]) : null;
item.longitude = parseFloat(arr[i - 2]) ? parseFloat(arr[i - 2]) : null;
item.wh_id = arr[i - 1];
item.case = arr[i];
} else {
item.mobile = arr[8]
item.locations = (arr[9] == "{}") ? null : arr[9];
item.ndc = !(arr[10] == 'f');
item.rdc = !(arr[11] == 'f');
item.latitude = parseFloat(arr[12]) ? parseFloat(arr[12]) : null;
item.longitude = parseFloat(arr[13]) ? parseFloat(arr[13]) : null;
item.wh_id = arr[14];
item.case = arr[15];
}
} else {
item.address = arr[6]
item.name = arr[7]
item.mobile = arr[8]
item.locations = (arr[9] == "{}") ? null : arr[9];
item.ndc = !(arr[10] == 'f');
item.rdc = !(arr[11] == 'f');
item.latitude = parseFloat(arr[12]) ? parseFloat(arr[12]) : null;
item.longitude = parseFloat(arr[13]) ? parseFloat(arr[13]) : null;
item.wh_id = arr[14];
item.case = arr[15];
}
process.nextTick(function() {
col_shipping.insert(item);
})
}
}, function() {
console.log('end')
db.close();
})
});
// lines.on('end', function() {
// MongoClient.connect(url, function(err, db) {
// items.splice(0, 1);
// col_shipping.insertMany(items);
// db.close();
// });
// });