-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongodb.js
173 lines (161 loc) · 3.97 KB
/
mongodb.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { MongoClient: mongoClient, ObjectID } = require('mongodb');
const url = 'mongodb://127.0.0.1:27017';
const databaseName = 'task-manager';
mongoClient.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
async (error, client) => {
if (error) {
return console.log('Something went wrong');
}
console.log('database connected successfully');
const db = client.db(databaseName);
// db.collection('users').insertOne(
// {
// name: 'indraraj',
// age: 23,
// },
// (error, result) => {
// if (error) {
// return console.log('Unable to insert user');
// }
// console.log(result.ops);
// },
// );
// db.collection('users').insertMany(
// [
// {
// name: 'indra',
// age: 32,
// },
// {
// name: 'alexa',
// age: 32,
// },
// ],
// (error, result) => {
// if (error) {
// return console.log('unable to insert documents');
// }
// console.log(result.ops);
// },
// );
/* db.collection('tasks').insertMany(
[
{
description: 'Buy an apple',
completed: true,
},
{
description: 'Buy an banana',
completed: false,
},
{
description: 'Get the gun',
completed: true,
},
],
(error, result) => {
if (error) {
return console.log('Unable to insert documents');
}
console.log(result.ops);
},
); */
/* db.collection('users').findOne({ name: 'alexa' }, (error, result) => {
if (error) {
return console.log('Unable to fetch');
}
console.log(result);
}); */
/* db.collection('users').findOne(
{ _id: new ObjectID('5dfcda9093519d6f400b57cd') },
(error, result) => {
if (error) {
return console.log('Unable to fetch');
}
console.log(result);
},
); */
/* db.collection('users')
.find({ age: 23 })
.skip(1)
.count((error, result) => {
console.log(result);
}); */
/* db.collection('tasks').findOne(
{ _id: new ObjectID('5dfdc4d212c4aa4f1c20cb6d') },
(error, result) => {
if (error) {
return console.log('unable to fetch');
}
console.log(result);
},
); */
/* db.collection('tasks')
.find({ completed: true })
.toArray((error, result) => {
if (error) {
return console.log('unable to fetch');
}
console.log(result);
}); */
/*
try {
const updateUser = await db.collection('users').updateOne(
{
_id: new ObjectID('5dfcda9093519d6f400b57cd'),
},
{
$set: {
age: 40,
},
},
);
if (updateUser) {
console.log('User Updated successfully');
}
} catch (err) {
console.log(err, 'Unable to update');
} */
/* try {
const updateUser = await db.collection('users').updateMany(
{
name: 'indraraj',
},
{
$set: {
age: 50,
},
},
);
if (updateUser) {
console.log('Users Updated successfully');
}
} catch (err) {
console.log(err, 'Unable to update users');
} */
/*
try {
const deleteUser = await db
.collection('users')
.deleteOne({ _id: new ObjectID('5dfcda9093519d6f400b57cd') });
if (deleteUser) {
console.log('User deleted successfully');
}
} catch (err) {
console.log(err, 'Unable to delete user');
}
*/
try {
const deleteUser = await db
.collection('users')
.deleteMany({ name: 'indraraj' });
if (deleteUser) {
console.log('Users deleted successfully');
}
} catch (err) {
console.log(err, 'Unable to delete users');
}
},
);