-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadCsv.js
80 lines (71 loc) · 2.33 KB
/
uploadCsv.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
process.on("message", async (message) => {
var data = message.csvData;
await mongoose.connect(URI, OPTS);
let IsProductExist = await Test.find({}); // Check Record Exist
let NewData = [];
if (IsProductExist.length > 0) { // Record Found Updating The old Records
data.map(async (e, i) => {
let temp = IsProductExist.find(element => element.sku === e.sku)
if (temp) {
// console.log(`updateOne({sku : "${temp.sku}"},{$set : {name : "${e.name}",description : "${e.description}"} })`)
await Test.updateOne({ sku: temp.sku }, { $set: { name: e.name, description: e.description } })
} else {
NewData.push(e)
}
temp = '';
}) // Bulk Inser the Records
if (NewData.length > 0) {
await Test.collection.insertMany(NewData);
}
} else { //Bulk Insert
let found = await Test.collection.insertMany(data);
}
// Getting The number of products with same Name
let NoOfAcc = await Test.aggregate([
{ $group: { _id: "$name", count: { $sum: 1 } } }
])
let IsRecordExist = await ProductCount.find({}) // Check product Count Exist
if(IsRecordExist.length > 0){
await mongoose.connection.db.dropCollection('productinfos')
}
let InsertCount = await ProductCount.collection.insertMany(NoOfAcc) // Bulk Insert Of Records
process.send(NoOfAcc); // Complete the Child Process
return connection.close();
})
//MongoDB Connection
const mongoose = require('mongoose');
const { Schema, connection } = mongoose;
const URI = 'mongodb://mongo:27017/products';
const OPTS = {
useNewUrlParser: true, useUnifiedTopology: true,
useCreateIndex: true
};
// Database Schema For Products
const schema = new Schema({
name: {
type: String,
required: true,
trim: true
},
sku: {
type: String,
required: true,
trim: true
},
description: {
type: String,
required: true,
trim: true
},
});
const Test = mongoose.model('Product', schema);
// Database Schema for Product counts
const productInfo = new Schema({
_id: {
type: String,
},
count: {
type: Number,
}
})
const ProductCount = mongoose.model('productInfo', productInfo);