npm install --save mongoose
var mongoose = require("mongoose");
mongoose.connect("mongodb://数据库的ip地址:端口号/数据库名", {useNewUrlParser:true})
注意:
mongoose.disconnect()
// 数据库连接成功的事件
mongoose.connection.once("open",function())
// 数据库断开的事件
mongoose.connection.once("close",function())
连接MongoDB数据库:
//引入
var mongoose = require("mongoose")
//连接数据库
mongoose.connect("mongodb://127.0.0.1/mongoose_test", {useNewUrlParser:true})
mongoose.connection.once("open",function () {
console.log("数据库连接成功~~~")
})
var mongoose = require("mongoose")
mongoose.connect("mongodb://127.0.0.1/mongoose_test", {useNewUrlParser:true})
mongoose.connection.once("open",function () {
console.log("数据库连接成功~~~")
})
//将mongoose.Schema赋值给一个变量
var Schema = mongoose.Schema;
//创建Schema(模式)对象
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female" //设置字段类型并设置默认值
},
address:String
})
//通过Schema来创建Model
//Model代表的是数据库中的集合,通过Model才能对数据库进行操作
//mongoose.model(modelName,Schema);
//modelName 就是映射的集合名。mongoose会自动将集合名变成数
var StuModel = mongoose.model("student",stuSchema);
//向数据库中插入一个文档
//StuModel.create(doc,function(err){});
StuModel.create({
name:"逍遥子",
age:18,
gender:"female",
address:"南山"
},function (err) {
if(err){
console.log("插入文档失败");
return;
}else {
console.log("插入成功");
}
})
//创建Schema(模式)对象
var qusSchema = new Schema({
author:String,
topic:String,
content:String,
openid:String,
avatural:String,
subject:String,
nickName:String,
time:String,
image:{
type:String,
default:"defaultimg.jpg"
},
})
//创建Schema(模式)对象
var stuSchema = new Schema({
name:String,
age:Number,
gender:{
type:String,
default:"female" //设置字段类型并设置默认值
},
address:String
})
var StuModel = mongoose.model("student",stuSchema);
//创建一个Document
var stu = new StuModel({
name: "孙悟空",
age: 19,
gender: "male",
address: "花果山"
})
stu.save(function (err) {
if(!err){
console.log("保存成功");
}
});
mongodbset.js
var mongoose = require("mongoose")
mongoose.connect("mongodb://127.0.0.1/fengwoForum", {useNewUrlParser:true})
mongoose.connection.once("open",function () {
console.log("数据库连接成功~~~")
})
//将mongoose.Schema赋值给一个变量
var Schema = mongoose.Schema;
exports.Schema = Schema;
exports.mongoose = mongoose;
testMap.js
var Schema = require("../module/mongodbset.js").Schema
var mongoose = require("../module/mongodbset.js").mongoose
//创建Schema(模式)对象
var qusSchema = new Schema({
author:String,
topic:String,
content:String,
openid:String,
avatural:String,
subject:String,
nickName:String,
time:String,
image:{
type:String,
default:"defaultimg.jpg"
},
})
var QusModel = mongoose.model("question",qusSchema);
module.exports = QusModel;
testController.js
var queModel = require("../database-map/testMap.js");
exports.addQuestion = function (callback) {
queModel.create({
author:"王顺顺666",
topic:"从前有个山",
content:"山上有个寺庙!",
openid:"10000001",
avatural:"https://www.xztywss.top/img/aaa.png",
subject:"大物2",
nickName:"庙里有个老和尚",
time:"2019/1/5 14/13",
image:"defaultimg.jpg"
},function (err) {
if(err){
callback(err);
return;
}else {
return;
}
})
}
exports.getQuestion = function (callback) {
queModel.find({},function(err,docs){
if(err){
callback(err,null)
console.log(err);
return;
}
callback(null,docs);
return;
})
}