-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
154 lines (124 loc) · 4.34 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
// CRUD create read update delete
// THIS FILE HAS NO CONNECTION/AFFILIATION WITH THE APP WE"RE BUILDING. IT'S A PERIPHERAL
const mongodb = require('mongodb')
// this returns an object
const MongoClient = mongodb.MongoClient
// this is one thing we need to initialize the connection.
const ObjectID = mongodb.ObjectId
// helps configure our preferred ids
// basic destructuring
// const {MongoClient, ObjectID} = mongodb
const connectionURL = 'mongodb://127.0.0.1:27017'
// this connects to our localhost server running in the mongodb terminal
// we could have just typed 'localhost:27017' but localhost is known to slow down applications and fail mostly. the reason is unknown. typing out the entire IP works just fine.
const databaseName = 'task-manager'
// can pick any name for this. name should most probably match the name of app.
// const id = new ObjectID()
// console.log(id);
// console.log(id.id);
// console.log(id.getTimestamp());
// console.log(id.toHexString())
MongoClient.connect(connectionURL, {
useNewUrlParser: true
// the original urlParser by default is being deprecated and it's required to pass in this option in order for our urls to be parsed correctly so we can connect to the server.
// takes a few arguments to set up the connection- the connectionURL, urlParser and a callback function(asynchronous)
}, (error, client) => {
if (error) {
return console.log('Unable to connect to database')
}
const db = client.db(databaseName)
// INSERT-ONE AND INSERT-MANY
// db.collection('users').insertOne({
// name: 'Vikran',
// age: 25
// }, (error, result)=> {
// if (error) {
// console.log('Unable to insert user')
// }
// console.log(result.insertedId) // result.ops wont work as it has been
// console.log(result.acknowledged) // removed from the new version
// })
// db.collection('users').insertMany([
// {
// name: 'Jen',
// age: 28
// }, {
// name: 'Gunther',
// age: 27
// }
// ], (error, result)=> {
// if (error) {
// console.log('Unable to insert documents')
// }
// console.log(result.insertedIds)
// console.log(result.acknowledged)
// })
// db.collection('tasks').insertMany([{
// description: 'Get the ball',
// completed: true
// }, {
// description: "Set the ball",
// completed: false
// }, {
// description: 'Shoot the ball',
// completed: true
// }], (error, result) => {
// if (error){
// return console.log('Unable to insert tasks')
// }
// console.log(result.insertedIds)
// console.log(result.acknowledged)
// })
// FIND-ONE AND FIND_MANY
// db.collection('users').findOne({_id: new ObjectID('63a09ea405e7a3ce379fcb76')}, (error, user)=> {
// if (error) {
// return console.log('Unable to fetch')
// }
// console.log(user)
// })
// db.collection('users').find({age: 27}).toArray((error, users)=>{
// if (error){
// console.log('Unable to fetch');
// }
// console.log(users)
// })
// db.collection('users').find({age:27}).count((error, count)=> {console.log(count)}) // returns the count of the obkects matching the parameter we're searching for
// challenge
// db.collection('tasks').findOne({ _id: ObjectID('63a090135f620e98c5b90bab') }, (error, user) => {
// if (error) {
// return console.log(error);
// }
// console.log(user)
// })
// db.collection('tasks').find({ completed: true }).toArray((error, tasks) => { console.log(users) })
// UPDATE_ONE AND UPDATE-MANY
// db.collection('users').updateOne({ _id: ObjectID('63a08216a5e557cae350d646') }, {
// $inc: {
// age: 1 // -1
// }
// }).then((result) => {
// console.log(result)
// }).catch(() => {
// console.log(error)
// })
// db.collection('tasks').updateMany({ completed: false }, {
// $set: {
// completed: true
// }
// }).then((result) => {
// console.log(result);
// }).catch((error) => {
// console.log(error);
// })
// DELETE_ONE AND DELETE_MANY
// db.collection('users').deleteMany({
// age: 27,
// }).then((result) => { console.log(result) }).catch((error) => {
// console.log(error)
// })
// db.collection('tasks').deleteOne({ completed: true }).then((result) => {
// console.log(result);
// }).catch((error) => {
// console.log(error);
// })
})