mongoose MongoDB
Node.js 资料库套件 mongoose MongoDB
Categories:
npm i mongoose
汇入
// Using Node.js `require()`
const mongoose = require('mongoose');
// Using ES6 imports
import mongoose from 'mongoose';
连线
const mongoose = require('mongoose');
const connectMongoDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
} catch (error) {
console.log(error);
}
}
module.exports = connectMongoDB;
事件
是否连线
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB');
})