-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.js
58 lines (47 loc) · 1.54 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
const {MongoClient, ObjectID} = require('mongodb')
const connectionURL = "mongodb://127.0.0.1:27018/"
const databaseName = "task-manager"
MongoClient.connect(connectionURL, {useNewUrlParser: true}, (error, client) => {
if(error){
return console.log('Unable to connect to database: ' + error)
}
const db = client.db(databaseName)
// const updatePromise = db.collection('users').updateOne({
// _id: new ObjectID("5cd8fe7952b959a13b28a75a"),
// },{
// $set : {name : "Jacob"}
// })
// updatePromise.then((result) =>{
// console.log(result)
// }).catch((error) => {
// console.log(error)
// })
const updatePromise = db.collection('users').updateOne({
_id: new ObjectID("5cd8fe7952b959a13b28a75a"),
},{
$set : {name : "Jacob"}
}).then((result) =>{ //// More concise syntax.
console.log(result)
}).catch((error) => {
console.log(error)
})
db.collection('tasks').updateMany({completed : false}, {
$set: {completed : true}
}).then((result)=>{
console.log(result)
}).catch((error) =>{
console.log(error)
})
db.collection('users').deleteMany({age: 20}).then( (result) => {
console.log(result)
}).catch( (error) => {
console.log(error)
})
db.collection('tasks').deleteOne(
{description : "Do more HW"}
).then( (result) => {
console.log(result)
}).catch( (error) => {
console.log(error)
})
})