1
0
CompleteNodeJS/task-manager/mongodb.js

35 lines
946 B
JavaScript
Raw Normal View History

// CRUD create read update delete
2019-10-28 00:40:26 +00:00
const { MongoClient, ObjectID } = require('mongodb')
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
const db = client.db(databaseName)
2019-10-28 00:40:26 +00:00
// db.collection('users').findOne({ _id: new ObjectID('5db632720a8c580049920f36') }, (error, user) => {
2019-10-27 19:38:19 +00:00
// if (error) {
2019-10-28 00:40:26 +00:00
// return console.log('Unable to fetch')
2019-10-27 19:38:19 +00:00
// }
//
2019-10-28 00:40:26 +00:00
// console.log(user)
2019-10-27 19:38:19 +00:00
// })
2019-10-28 00:40:26 +00:00
// db.collection('users').find({ age: 38 }).toArray((error, users) => {
// console.log(users)
2019-10-27 19:38:19 +00:00
// })
2019-10-28 00:40:26 +00:00
db.collection('tasks').findOne({ _id: new ObjectID('5db5f0ef9d71180110fd3162') }, (error, task) => {
console.log(task)
})
db.collection('tasks').find({ completed: false }).toArray((error, tasks) => {
console.log(tasks)
})
})