-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo.js
executable file
·37 lines (34 loc) · 863 Bytes
/
mongo.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
const config = require('./config/index');
const mongoose = require('mongoose');
const Product = require('./Product');
const products = require('./products');
mongoose.Promise = global.Promise;
mongoose
.connect(config.mongoUri, {
useMongoClient: true
})
.then(() => {
console.log('Mongodb is connected!!');
})
.catch(err => {
console.warn(err);
});
const productPromises = products.map(prod => {
const productData = {
name: prod.name,
category: prod.category,
brand: prod.brand,
price: prod.price,
quantity: prod.quantity
};
if (prod.model) {
productData.model = prod.model;
}
const product = new Product(productData);
return product.save();
});
Promise.all(productPromises)
.then(savedProducts => {
console.log(savedProducts.length + ' products have been saved!');
})
.catch(console.log);