Simple connect
suggest changeThe 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();
});
});
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents