直接開始示例代碼
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: String,
password: String
})
const Models = {
User: mongoose.model('User', UserSchema)
}
mongoose.connect('mongodb://127.0.0.1/database', { useMongoClient: true }) // 加上后面的{ useMongoClient: true }可以去除警告
const db = mongoose.connection
db.on('error',console.error.bind(console,'Database connection error.'));
db.once('open', () => {
console.log('The database has connected.')
});
module.exports = Models