-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodbDataTransfer.js
75 lines (65 loc) · 2.58 KB
/
mongodbDataTransfer.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
var mongoClient = require("mongodb").MongoClient;
var moment = require("moment");
var constring = "";
mongoClient.connect(constring, function(err, db) {
if (err) {
console.log("bağlantı hatası");
return;
}
db.collection("events", function(err, collection) {
if (err) {
console.log("collection alınamadı.");
return;
}
var options = {
"sort":[["startDate","asc"]]
};
collection.find({},options).toArray((err,docs) => {
console.log("documentlar alındı.");
docs.forEach(doc=>{
var startDate = moment(doc.startDate).format("YYYY-MM-DD");
var startTime = doc.startTime;
var endTime = doc.endTime;
if(!startTime){
console.log("Invalid StartTime - ",startTime);
return;
}
else if(!endTime){
console.log("Invalid EndDate - ", endTime);
}
startTime = startTime.toString();
endTime = endTime.toString();
if(startTime.length === 2){
startTime += ":00";
}
else if(startTime.length === 4){
startTime = startTime.replace(".",":") + "0";
}
else{
startTime = startTime.replace(".",":");
}
if(startTime === "Invalid date"){
console.log(JSON.stringify(doc));
}
if(endTime.length === 2){
endTime += ":00";
}
else if(endTime.length === 4){
endTime = endTime.replace(".",":") + "0";
}
else{
endTime = endTime.replace(".",":");
}
if(endTime === "Invalid date"){
console.log(JSON.stringify(doc));
}
var startDateTime = moment(startDate + " " + startTime);
var endDateTime = moment(startDate + " " + endTime);
// startDateTime = startDateTime;
// endDateTime = endDateTime;
console.log("ID: ",doc._id.toString(), " - StartDate: ",moment(startDateTime).format("YYYY-MM-DD HH:mm"), " - EndDate: ", moment(endDateTime).format("YYYY-MM-DD HH:mm") );
collection.update({ _id: doc._id }, { $set: { endDate: endDateTime._d, startDate: startDateTime._d }});
});
});
});
});