-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileMongoByFile.js
51 lines (30 loc) · 1018 Bytes
/
fileMongoByFile.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
"use strict";
var fs = require('fs');
var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;
var url = 'mongodb://localhost:27017/WebCastells';
var db;
MongoClient.connect(url, function(err, tdb) {
db = tdb;
fs.readFile('./files/Alçades Colla.csv', 'utf8', function (err, data) {
if (err) throw err;
var fileLines = data.split("\r\n ");
var leyend = fileLines[0].split(";");
var users = [];
for(var i=1 ; i< fileLines.length; i++){
var temp = fileLines[i].trim().split(";");
var json = {};
for(var j in leyend){
if(leyend[j] == "role"){
json[leyend[j]] = temp[j].trim().split(",");
}else {
json[leyend[j]] = temp[j].trim();
}
}
users.push(json);
}
console.log(users);
var collection = db.collection('pinyeros');
collection.insertMany(users);
});
});