1
0
Fork 0
CompleteNodeJS/task-manager/mongodb.js

35 lines
946 B
JavaScript

// CRUD create read update delete
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)
// db.collection('users').findOne({ _id: new ObjectID('5db632720a8c580049920f36') }, (error, user) => {
// if (error) {
// return console.log('Unable to fetch')
// }
//
// console.log(user)
// })
// db.collection('users').find({ age: 38 }).toArray((error, users) => {
// console.log(users)
// })
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)
})
})