Simple connect

suggest change

The callback form below is the MongoDB Node driver 3 API. Driver 4 removed the connect callback. connect gives you a client. The database name is an argument to client.db(), not a property of the client. insert was removed; use insertOne.

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

MongoClient.connect('mongodb://localhost:27017', function (error, client) {
  if (error) return console.log(error);
  const collection = client.db('databaseName').collection('collectionName');
  collection.insertOne({ key: 'value' }, function (error, result) {
    console.log(error, result);
    client.close();
  });
});

Feedback about page:

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



Table Of Contents