Simple connect using promises

suggest change

This uses the MongoDB Node driver 4 or later. MongoClient.connect resolves to a client. Choose the database with client.db(). insertOne replaces the old insert.

const { MongoClient } = require('mongodb');

const client = new MongoClient('mongodb://localhost:27017');

client.connect()
  .then(function () {
    const collection = client.db('databaseName').collection('collectionName');
    return collection.insertOne({ key: 'value' });
  })
  .then(function (result) {
    console.log(result);
    return client.close();
  })
  .catch(function (error) {
    console.error(error);
  });

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents