-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
104 lines (88 loc) · 2.61 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
// CRUD create read update delete
// const mongodb = require('mongodb')
// const MongoClient = mongodb.MongoClient
// const mongoID = mongodb.mongoID
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.toHexString())
// console.log(id.toString())
// console.log(id.getTimestamp())
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error , client) => {
if (error) {
return console.log('unable to connect to database')
}
const db = client.db(databasename)
// db.collection('users').insertMany([
// {
// name: 'prerit',
// age:19
// },{
// name:'abcdefg',
// age:19
// }
// ], (error,result) => {
// if (error) {
// return console.log('unable to insert user')
// }
// console.log(result.ops)
// })
// db.collection('users').findOne({ _id: new ObjectId("604f5c412f57c73cabe838b6"), age:1}, (error,user) => {
// if (error){
// return console.log('error')
// }
// console.log(user)
// })
// db.collection('users').find({age:19}).toArray((error , users) =>{
// console.log(users)
// })
// db.collection('users').find({age:19}).count((error , users) =>{
// console.log(users)
// })
// db.collection('tasks').findOne({ _id: new ObjectId("604f5d3abaf3223cd2bb1bb8")}, (error,task) =>{
// if (error) {
// return console.log(1)
// }
// console.log(task)
// })
// db.collection('tasks').find({completed:false}).toArray((error , users) =>{
// if (error) {
// return console.log(2)
// }
// console.log(users)
// })
// db.collection('users').updateOne({
// _id: new ObjectId("604f59c53442973c592ef295")
// },{
// $inc:{
// age:1
// }
// // $set: {
// // name:"qwert"
// // }
// }).then((result) =>{
// console.log(result)
// }).catch((error) =>{
// console.log(error)
// })
// db.collection('tasks').updateMany({
// completed:true
// },{
// $set: {
// completed:false
// }
// }).then((result) =>{
// console.log(result)
// }).catch((error) =>{
// console.log(error)
// })
db.collection('users').deleteMany({
age:19
}).then((result) =>{
console.log(result)
}).catch((error) => {
console.log(error)
})
})