-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongodb.js
More file actions
100 lines (83 loc) · 2.6 KB
/
mongodb.js
File metadata and controls
100 lines (83 loc) · 2.6 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
//CRUD operations
const { MongoClient, ObjectID } = require('mongodb')
// const {MongoClient, ObjectId} = mongodb.MongoClient
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager-api'
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
const db = client.db(databaseName)
if (!db) {
throw new Error('ops!! something went wrong, while coonecting to datbase!')
}
// db.collection('users').insertOne({
// name: 'Andrew',
// age: 27
// })
// db.collection('tasks').insertMany([{
// description: 'Clean the House',
// completed:true
// },
// {
// description: 'complete this api by tonight',
// completed:false
// }], (error, result) => {
// if (error) {
// return console.log('ops! cant insert document!' )
// }
// console.log(result.ops)
// })
// db.collection('tasks').find({ completed: false }).toArray((error, tasks) => {
// console.log(tasks)
// })
// db.collection('tasks').findOne({
// _id: new
// ObjectID("5c95159a5219a125e1ed2341")
// }, (error, task) => {
// console.log(task)
// })
// db.collection('users').updateOne({
// _id: new ObjectID("5c9514320ad8af2499ad4392")
// },
// {
// $inc: {
// age: 1
// },
// }).then((result) => {
// // console.log(result)
// db.collection('users').findOne({
// _id: new
// ObjectID("5c9514320ad8af2499ad4392")
// }, (error, user) => {
// console.log(user)
// })
// }).catch((error) => {
// console.log(error)
// })
// db.collection('tasks').updateMany({
// completed: false
// }, {
// $set: {
// completed: true
// }
// }).then((result) => {
// console.log(result.modifiedCount)
// }).catch((error) => {
// console.log(error)
// })
// db.collection('users').deleteMany({
// age: 32
// }).then((result) => {
// console.log(result)
// }).catch((error) => {
// console.log(error)
// })
// db.collection('tasks').deleteOne({
// description: "Clean the House"
// }).then((result) => {
// console.log(result.deletedCount)
// }).catch((error) => {
// console.log(error)
// })
})