我刚刚开始学习 MongoDB,我正在尝试通过 MongoDB Server 6.0 在本地托管我的 Node.js 应用程序(不使用Mongoose或Atlas)。
我复制了 MongoDB 文档中给出的异步 JavaScript 代码。我确保在执行以下代码之前运行mongod :
MongoDB 服务器已启动

const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"**mongodb://localhost:27017**";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("Connected successfully to server");
} finally { …Run Code Online (Sandbox Code Playgroud)