-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek03.js
89 lines (79 loc) · 2.72 KB
/
week03.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
81
82
83
84
85
86
87
88
89
require("sslkeylog").hookAll();
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const uri = "mongodb+srv://m001-student:m001-mongodb-basics@sandbox.2ozfn.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
// client.connect(err => {
// if(err) {
// console.log(err.message)
// return
// }
// console.log('Connected to MongoDB');
// client.db('myFirstDatabase').collection('customers').insertOne({
// name: 'John',
// age: 30,
// location: 'New York',
// isActive: true,
// tags: ['tag1', 'tag2'],
// }).then(result => {
// console.log(result);
// });
// client.db('myFirstDatabase').collection('customers').insertOne({
// name: 'Johns Friend',
// friend: result.insertedId,
// age: 30,
// location: 'New York',
// isActive: true,
// tags: ['tag1', 'tag2'],
// }).then(result => {
// console.log(result);
// });
client.connect(async err => {
if(err) {
console.log(err.message)
return
}
console.log('Connected to MongoDB');
// console.time('insert')
// let result1 = await client.db('myFirstDatabase').collection('customers').insertMany([
// {
// name: 'Ali',
// age: 30,
// location: 'New York',
// isActive: true,
// tags: ['tag1', 'tag2'],
// },
// {
// name: 'John',
// age: 40,
// location: 'Melaka',
// isActive: true,
// tags: ['tag1', 'tag2'],
// }
// ])
// console.timeEnd('insert')
// let result2 = await client.db('myFirstDatabase').collection('customers').insertOne({
// name: 'Alis Friend',
// friend: result1.insertedId,
// age: 30,
// location: 'New York',
// isActive: true,
// tags: ['tag1', 'tag2'],
// })
// console.log('Inserted 1 document', result);
// await client.db('myFirstDatabase').collection('customers').updateOne({
// _id: ObjectId("6239c0dc5c99a790e3a692ac") // name: 'John'
// },{
// $set: {
// name: "John Doe",
// age: 35
// }
// },{ upsert: true }).then(res => {
// console.log(res)
// })
client.db('myFirstDatabase').collection('customers').deleteOne({
name: 'John',
age: 30
}).then(result => {
console.log(result.deletedCount);
})
});