示例代码:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to MongoDB', err));
此示例代码连接到名为“mydatabase”的本地Mongodb实例。如果连接失败,则会打印错误消息。