-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodb.js
More file actions
165 lines (152 loc) · 4.06 KB
/
Copy pathmongodb.js
File metadata and controls
165 lines (152 loc) · 4.06 KB
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
//crud - create read update delete
const mongodb=require('mongodb')
const mongoClient=mongodb.MongoClient
const ObjectID=mongodb.ObjectID
//const id=new ObjectID; //it generates a object id
//console.log(id.id)
const connectionURL=process.env.MONGODB_URL //127.0.0.1 is the local host ip and 27017 is the default mongodb port
const databaseName='task-manager' //database name
mongoClient.connect(connectionURL,{useNewUrlParser:true,useUnifiedTopology: true},(error,client)=>
{
if(error)
{
return console.log('unable to connect')
}
const db=client.db(databaseName) //here in mongodb no need to create database use the database inside this to proceed further
/* db.collection('users').insertOne({ //inserting a collection inside db
name:'satwik',
age:21
},(error,result)=> //callback function is called when an operation is completed
{
if(error)
{
return console.log(error)
}
return console.log(result.ops) //ops property gives the array of all the documents that we have inserted
}) */
/* db.collection('users').insertMany([
{
name:'deepak',
age:20
},
{
name:'ishan',
age:19
}
],(error,result)=>
{
if(error)
{
return console.log(error)
}
return console.log(result.ops)
}) */
/* db.collection('new-tasks').insertMany([
{
task:'studying',
status:false
},
{
task:'playing',
status:true,
},
{
task:'going to gym',
status:true
}
],(error,result)=>
{
if(error)
{
return console.log(error)
}
return console.log(result.ops)
}) */
/* db.collection('users').findOne({_id:new ObjectID("5df06d1fdbc0d244742e9d24")},(error,result)=>{ //to find documents by seaching through objectid
if(error)
{
return console.log(error)
}
console.log(result)
})
//find is different than findOne or insert or insertOne.The reason is that find does not accept a callback function as it does not returns the documents ..it returns a cursor that points to the document
db.collection('users').find({age:21}).toArray((error,users)=>{ //to get an array of documents returned by the curesor we use toArray
if(error)
{
return console.log(error)
}
console.log(users)
})
db.collection('users').find({age:21}).count((error,count)=>{ //to get a count of the documents stored.This is where cursors are used .It directly fetched us the count instead of first loading it into the node
if(error)
{
return console.log(error)
}
console.log(count)
}) */
/* db.collection('new-tasks').findOne({_id:new ObjectID('5df1e79d8d900925702edb4d')},(error,result)=>
{
if(error)
{
return console.log(error)
}
console.log(result)
})
db.collection('new-tasks').find({status:false}).toArray((error,users)=>
{
if(error)
{
return console.log(error)
}
console.log(users)
})
*/
/*db.collection('users').updateOne({
_id:new ObjectID("5df1db37246744330c7eb74a")
},
{
$set:{
name:'abhinav'
}
}).then((result)=>
{
console.log(result)
}).catch(()=>
{
console.log(error)
}) */
/*db.collection('users').updateOne({
_id:new ObjectID("5df1db37246744330c7eb74a")
},
{
$set:{
name:'Mark'
}
},(error,result)=> //used callback here for the same function which is done above using promise
{
console.log(result)
})*/
/*db.collection('new-tasks').updateMany({
status:false
},
{
$set:{
status:true
}
}).then((result)=>
{
console.log(result)
}).catch((error)=>
{
console.log(error)
})*/
db.collection('new-tasks').deleteOne({
task:'playing'
}).then((result)=>
{
console.log(result)
}).catch((error)=>
{
console.log(error)
})
})