-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
116 lines (86 loc) · 2.47 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
//crud
//destructured these down below
// const mongodb = require('mongodb');
// const MongoClient = mongodb.MongoClient;
// const ObjectID = mongodb.ObjectID;
//destructuring result
const { MongoClient, ObjectID } = require('mongodb');
const id = new ObjectID();
console.log(id)
const connectionUrl = 'mongodb://127.0.0.1:27017';
const databaseName = 'task-manager';
MongoClient.connect(connectionUrl, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('unable to connect');
}
const db = client.db(databaseName);
//an example of one document with id
// db.collection('users').insertOne({
// _id: id,
// name: 'lisa',
// address: 'plaka'
// }, (error, response) => {
// console.log(response.ops);
// })
//FINDONE
// db.collection('users').findOne({ name: 'lisa' }, (error, user) => {
// if (error) {
// console(erro);
// }
// console.log(user);
// });
//FIND
// db.collection('tasks').find({ completed: true }).toArray((error, location) => {
// if (error) {
// console.log(error);
// }
// console.log(location);
// });
//FINDONE TO FETCH BY ID
db.collection('tasks').find({ _id: new ObjectID("5d248bb2df5a106a0caee63a") }).toArray((error, id) => {
console.log(id);
})
// an example of many
// db.collection('tasks').insertMany([
// {
// description: 'update file',
// completed: true
// },
// {
// description: 'delete file',
// completed: false
// },
// {
// description: 'create file',
// completed: true
// }
// ], (error, response) => {
// if (error) {
// console.log(error);
// }
// console.log(response.ops);
// })
/////////////////UPDATE MANY
// db.collection('tasks').updateMany(
// {
// completed: true,
// },
// {
// $set: {
// completed: false
// }
// }
// ).then((result) => {
// console.log(result);
// }).catch((err) => {
// })
///////////////DELET one
db.collection('tasks').deleteOne(
{
description: 'update file'
}).then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
});