-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
168 lines (138 loc) · 4.11 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
//CRUD - Create Read Update Delete
// const mongodb = require('mongodb')
// const MongoClient = mongodb.MongoClient
// const ObjectId = mongodb.ObjectID
const { MongoClient, ObjectID} = require('mongodb')
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'
// const id = new ObjectID()
// console.log(id)
// console.log(id.getTimestamp())
// //buffer - binary data
// console.log(id.id)
// console.log(id.id.length)
//console.log(id.toHexString().length)
MongoClient.connect(connectionURL, {useNewUrlParser: true}, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
console.log('Connected to Database!')
const db = client.db(databaseName)
//deleting data from documents
// db.collection('users').deleteMany({
// age: 24
// }).then((result)=>{
// console.log(result)
// }).catch((error)=>{
// console.log(error)
// })
// db.collection('tasks').deleteOne({
// description: 'Finish cours'
// }).then((result)=>{
// console.log(result)
// }).catch((error)=>{
// console.log(error)
// })
//updating Documents in collection
// const updatePromise = db.collection('users').updateOne({
// _id: new ObjectID("5ed770193f376365babb1254")
// }, {
// $set: {
// name: 'Asha',
// age: 26
// },
// $inc: {
// age: 27
// }
// })
// updatePromise.then((result)=> {
// console.log('Success',result)
// }).catch((error) => {
// console.log('Error', error)
// })
// db.collection('tasks').updateMany({
// completed: false
// }, {
// $set: {
// completed: true
// }
// }).then((result) => {
// console.log('Success', result)
// }).catch((error) => {
// console.log('Error!', error)
// })
//fetching data from database
// db.collection('users').findOne({name: 'Patricia', age: 44} , (error, user) => {
// if (error)
// {
// return console.log('Unable to fetch')
// }
// console.log(user)
// })
// db.collection('users').findOne({ _id: new ObjectID("5ed761ea7e99554fc6a0a3ca")} , (error, user) => {
// if (error)
// {
// return console.log('Unable to fetch')
// }
// console.log(user)
// })
// db.collection('users').find({ age : 25}).toArray((error, users) => {
// console.log(users)
// })
// db.collection('users').find({ age : 25}).count((error, count) => {
// console.log(count)
// })
// db.collection('tasks').find({ completed : false}).toArray((error, tasks) => {
// console.log(tasks)
// })
//insert single record in DB
// db.collection('users').insertOne({
// _id : id,
// name : 'Patricia',
// age : 24
// },
// (error, result) => {
// if (error)
// {
// return console.log('Unable to insert user')
// }
// console.log(result.ops)
// })
//insert multiple records in DB
// db.collection('users').insertMany([
// {
// name: ' Eva',
// age: 22
// } ,
// {
// name: 'Anabella',
// age: 26
// }
// ] , (error, result) => {
// if (error)
// {
// return console.log('Unable to insert documents!')
// }
// console.log(result.ops)
// })
// db.collection('tasks').insertMany([
// {
// description: 'Clean House',
// completed: true
// } ,
// {
// description: 'Pick up packagae',
// completed: false
// },
// {
// description: 'Finish cours',
// completed: false
// }
// ] , (error, result) => {
// if (error)
// {
// return console.log('Unable to insert tasks!')
// }
// console.log(result.ops)
// })
})