Mongoose添加验证Node-Demo连接方式
# mongodb://user:pwd@host/database-name?authSource=admin
mongodb://root:root@127.0.0.1:27017/school?authSource=admin
-
未添加
?authSource=admin
-
添加后
-
mongoose 验证连接方式
mongodb://root:root@127.0.0.1/Teacher?authSource=admin
// 导入模块
const mongoose = require("mongoose");
// 连接数据库
const db = mongoose.createConnection(
"mongodb://root:root@127.0.0.1:27017/school?authSource=admin",
// mongodb[协议]://user:password@host:port/database-name
{ useNewUrlParser: true, useUnifiedTopology: true },
(err) => {
if (err) {
console.log("-------------------------------------");
console.log("数据库连接失败: ", err);
console.log("-------------------------------------");
} else {
console.log("数据库连接成功");
}
}
);
// 创建集合规则
const teacherSchema = new mongoose.Schema({
// 教师: 课程 班级 上课周期 是否休假
course: String,
classroom: String,
ontime: String,
isSleep: Boolean,
});
// 使用规则创建集合 创建集合 Teacher,使用 teacherSchema 规则
const Teacher = mongoose.model("Teacher", teacherSchema);
// 创建集合实例
const teacher = new Teacher({
course: "node.js",
classroom: "1801",
ontime: "12:00",
isSleep: true,
});