This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.js
45 lines (35 loc) · 2.15 KB
/
create_db.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
/*
* For creating database, collections and product data in MongoDB
*/
const { MongoClient } = require("mongodb");
const client = new MongoClient("mongodb://root:example@localhost:27017?authMechanism=DEFAULT");
async function run() {
try {
await client.connect();
const database = client.db("mydb");
console.log("DB created");
await database.createCollection("products");
await database.createCollection("orders");
console.log("Collections created");
const data = [
{ "id": 1, "name": "Kayak", "category": "Watersports", "description": "A boat for one person", "price": 275 },
{ "id": 2, "name": "Lifejacket", "category": "Watersports", "description": "Protective and fashionable", "price": 48.95 },
{ "id": 3, "name": "Soccer Ball", "category": "Soccer", "description": "FIFA-approved size and weight", "price": 19.5 },
{ "id": 4, "name": "Corner Flags", "category": "Soccer", "description": "Give your playing field a professional touch", "price": 34.95 },
{ "id": 5, "name": "Stadium", "category": "Soccer", "description": "Flat-packed 35,000-seat stadium", "price": 79500 },
{ "id": 6, "name": "Thinking Cap", "category": "Chess", "description": "Improve brain efficiency by 75%", "price": 16 },
{ "id": 7, "name": "Unsteady Chair", "category": "Chess", "description": "Secretly give your opponent a disadvantage", "price": 29.95 },
{ "id": 8, "name": "Human Chess Board", "category": "Chess", "description": "A fun game for the family", "price": 75 },
{ "id": 9, "name": "Bling Bling King", "category": "Chess", "description": "Gold-plated, diamond-studded King", "price": 1200 },
];
const products = database.collection("products");
if (await products.countDocuments() > 0) return;
const result = await products.insertMany(data, { ordered: true });
console.log(`${result.insertedCount} documents inserted`);
} catch (err) {
console.log(err);
} finally {
await client.close();
}
}
run().catch(console.dir)