从MongoDB 5.0开始,MongoDB弃用了SDAM引擎。 替代该引擎的是Mongoclient。 使用MongoClient可以通过以下方式进行:
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017/';
const client = new MongoClient(uri, { useUnifiedTopology: true });
async function run() {
try {
await client.connect();
console.log('Connected to the server');
await client.db('test').command({ ping: 1 });
console.log('Pinged successfully');
} catch (err) {
console.log(err);
} finally {
await client.close();
}
}
run().catch(console.dir);
这将连接到mongod实例并发送ping。 请注意,示例中使用了useUnifiedTopology选项。 这是因为默认情况下不使用它会收到警告。