-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
140 lines (121 loc) · 3.14 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
const mongodb = require('mongodb');
// const MongoClient = mongodb.MongoClient; //initialize an instance of mongodb client
// const ObjectID = mongodb.ObjectID;
const {MongoClient, ObjectID} = require('mongodb');
const connectionURL = 'mongodb://127.0.0.1:27017';
const databaseName = 'taskmanager';
//const id = new ObjectID(); //initialize new instance of ID
MongoClient.connect(connectionURL, {useNewUrlParser: true}, (error,client)=>{
if(error){
return console.log('unable to connect to database!');
}
const db = client.db(databaseName); //initialize an instance of a connection to desired database;
db.collection('tasks').deleteOne({
description:'pay bills'
}).then((result)=>{
console.log(result);
}).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').updateOne({
// _id: new ObjectID('5cea39d689d9301d108d01f6')
// }, {
// $inc:{
// age: 1
// }
// }).then((result)=>{
// console.log(result.modifiedCount);
// }).catch((error)=>{
// console.log(error);
// })
//
// db.collection('users').findOne({
// _id: new ObjectID('5cea3effc0e0424484291918')
// }, (error, user)=>{
// if(error){
// console.log('unable to fetch user!');
// }
// console.log(user);
// })
// db.collection('users').find({
// name: "yongjoong"
// }).toArray((error,users)=> {
// console.log(users);
// })
// db.collection('tasks').findOne({
// _id: new ObjectID('5cea4069bfe34742348de39f')
// }, (error, task)=>{
// if(error){
// return console.log('could not find task!');
// }
// console.log(task);
// })
//
// db.collection('tasks').find({
// completed: false
// }).toArray((error, tasks)=>{
// if(error){
// return console.log('could not find tasks!');
// }
// console.log(tasks);
// })
// db.collection('users').insertOne({ //insert 1 object into collection(tables in SQL) labelled users
// _id: id,
// name: 'robert',
// age: 33
// }, (error, result)=>{ //callback function after connecting
// if(error){
// return console.log('unable to insert');
// }
//
// console.log(result.ops);
// });
// db.collection('users').insertMany(
// [
// {
// name: 'tom',
// age: 22
// },
// {
// name: 'kore',
// age: 25
// }
// ]
// ,(error,result)=>{
// if(error){
// return console.log('unable to add users!');
// }
//
// console.log(result.ops);
// })
// db.collection('tasks').insertMany([
// {
// description: 'pay bills',
// compeleted: false
// },
// {
// description: 'feed cat',
// completed: true
// },
// {
// description: 'make dinner',
// completed: false
// }
// ], (error, result)=>{
// if(error){
// return console.log('unable to add tasks!');
// }
// console.log(result.ops)
// })
})