const { MongoClient, ObjectID } = require('mongodb');
const debug = require('debug')('mongodb-connect');
MongoClient.connect('mongodb://localhost:27017/TodoApp', { useNewUrlParser: true }, (err, client) => {
if (err) return debug(`Unable to connect to the server ${err}`);
debug('Connected to the server');
const db = client.db('TodoApp');
db.collection('Todos').insertOne({
text: 'Something to do',
completed: false,
}, (error, result) => {
if (err) return debug(`There was a problem while inserting, ${error}`);
debug(`Data inserted successfully ${JSON.stringify(result.ops, undefined, 2)}`);
});
client.close();
});
Now in the above code, I've an object of MongoClient and I called MongoClient.connect() method to connect my Node app with my local database server. In my callback I get another client object which I used to perform the database operations. I'm confused between differentiating both objects: MongoClient and client(from the callback)