Currently, we have two suggestions:
const { MongoClient } = require('mongodb');
|
const assert = require('assert');
|
|
// Connection URL
|
const url = 'mongodb://localhost:27017';
|
|
// Database Name
|
const dbName = 'myproject';
|
|
// Create a new MongoClient
|
const client = new MongoClient(url);
|
|
async function main() {
|
// Use connect method to connect to the Server
|
await client.connect();
|
|
try {
|
// content here
|
} finally {
|
await client.close();
|
}
|
}
|
|
main().catch(console.dir);
|
const { MongoClient } = require('mongodb');
|
const assert = require('assert');
|
|
// Connection URL
|
const url = 'mongodb://localhost:27017';
|
|
// Database Name
|
const dbName = 'myproject';
|
|
// Create a new MongoClient
|
const client = new MongoClient(url);
|
|
// Use connect method to connect to the Server
|
client.connect().then(async function() {
|
try {
|
// content here
|
} finally {
|
await client.close();
|
}
|
});
|