1. 下载mongoose模块
cmd后台输入:npm install mongoose --save
2. mongoose使用
//引入mongoose模块
var mongoose = require("mongoose")
//连接mongo数据库
mongoose.connect("mongodb://localhost:27017/project")
var db = mongoose.connection
//连接数据库成功后给后台发送连接信息
db.on("open",(err)=>{
if(err) throw err
console.log("connect success")
})
//定义数据结构
var userSchema = new mongoose.Schema({
username:String,
id:String,
password:String,
age:Number
},{
versionKey:false
})
var newsSchema = new mongoose.Schema({
id:String,
title:String,
content:String
},{
versionKey:false
})
//定义数据模型
var userModel = mongoose.model("user",userSchema,"userList")
var newsModel = mongoose.model("news",newsSchema,"newsList")
//导出
module.exports = {
userModel:userModel,
newsModel:newsModel
}