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