From e4662eb7293d9abb80bcc5727368edbc26028e62 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 27 Oct 2019 18:40:26 -0600 Subject: [PATCH] Video 78: Querying Documents --- task-manager/mongodb.js | 50 +++++++++-------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/task-manager/mongodb.js b/task-manager/mongodb.js index eded13e..a59da0a 100644 --- a/task-manager/mongodb.js +++ b/task-manager/mongodb.js @@ -1,7 +1,6 @@ // CRUD create read update delete -const mongodb = require('mongodb') -const MongoClient = mongodb.MongoClient +const { MongoClient, ObjectID } = require('mongodb') const connectionURL = 'mongodb://127.0.0.1:27017' const databaseName = 'task-manager' @@ -13,50 +12,23 @@ MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => const db = client.db(databaseName) - // db.collection('users').insertOne({ - // name: 'Jason', - // age: 38 - // }, (error, result) => { + // db.collection('users').findOne({ _id: new ObjectID('5db632720a8c580049920f36') }, (error, user) => { // if (error) { - // return console.log('Unable to insert user') + // return console.log('Unable to fetch') // } // - // console.log(result.ops) + // console.log(user) // }) - // db.collection('users').insertMany([ - // { - // name: 'Jen', - // age: 28 - // }, - // { - // name: 'Gunther', - // age: 27 - // } - // ], (error, result) => { - // if (error) { - // return console.log('Unable to insert documents') - // } - // - // console.log(result.ops) + // db.collection('users').find({ age: 38 }).toArray((error, users) => { + // console.log(users) // }) - db.collection('tasks').insertMany([ - { - description: 'Install MongoDB', - completed: true - },{ - description: 'Complete the challenge for Video 76', - completed: false - },{ - description: 'Learn NodeJS', - completed: false - } - ], (error, result) => { - if (error) { - return console.log('Unable to insert documents') - } + db.collection('tasks').findOne({ _id: new ObjectID('5db5f0ef9d71180110fd3162') }, (error, task) => { + console.log(task) + }) - console.log(result.ops) + db.collection('tasks').find({ completed: false }).toArray((error, tasks) => { + console.log(tasks) }) })