1. 安裝mongoose
npm install mongoose --save
2.創(chuàng)建mongoose連接
在model文件夾中,創(chuàng)建db.js文件用于創(chuàng)建數(shù)據(jù)庫(kù)的連接對(duì)象瘫寝,然后將創(chuàng)建好的連接對(duì)象曝露出去民鼓。
// 引包
const mongoose = require("mongoose");
//連接數(shù)據(jù)庫(kù)
// mongoose.connect("mongodb://localhost/mongooseDB");
var db=mongoose.createConnection("mongodb://localhost/mongooseDB");
db.once('open',function(callback){
console.log('數(shù)據(jù)庫(kù)連接ok...');
})
//將創(chuàng)建連接的對(duì)象曝露出去
module.exports=db;
3. mongoose操作對(duì)象
Mongoose 里矛物,一切都始于Schema,它可以看做是一種架構(gòu)路翻,每一個(gè)創(chuàng)建的架構(gòu)(Schema)都會(huì)映射到MongoDB中collection狈癞,并且創(chuàng)建與Schema相同的結(jié)構(gòu)的文檔(數(shù)據(jù))。
我們繼續(xù)在model文件夾中創(chuàng)建一個(gè)student.js文件茂契,用于搭建學(xué)生的架構(gòu)(Schema)蝶桶,創(chuàng)建完成后我們使用db.model將這個(gè)架構(gòu)映射成一個(gè)學(xué)生模型,最后將這個(gè)模型也導(dǎo)出掉冶。
//引包
var mongoose=require('mongoose');
var db=require('./db')
//創(chuàng)建一個(gè)學(xué)生架構(gòu)
var studentSchema = mongoose.Schema({
name: {
type: String,
default: '匿名用戶'
},
age: {
type: Number
},
sex: {
type: Boolean
}
})
//將學(xué)生架構(gòu)映射成一個(gè)學(xué)生(Student)模型
var studentModel=db.model('Student',studentSchema);
//將學(xué)生模型曝露出去
module.exports=studentModel;
4. 創(chuàng)建實(shí)例并保存
創(chuàng)建一個(gè)app.js當(dāng)做程序的入口文件真竖,這里引用剛才導(dǎo)出的學(xué)生文件,獲得一個(gè)學(xué)生的模型厌小,然后創(chuàng)建這個(gè)模型對(duì)象恢共,相當(dāng)于實(shí)例化一個(gè)學(xué)生對(duì)象。
var student = require('./model/student')
//創(chuàng)建對(duì)象
var admin= new student({
name: 'admin',
age: 33,
sex: true
});
//保存
admin.save(function () {
console.log('ok');
})
//創(chuàng)建并保存另一個(gè)對(duì)象
student.create({
name: 'test',
age: 18,
sex: false
})
運(yùn)行測(cè)試
npm app.js
查看數(shù)據(jù)庫(kù)中獲得兩條數(shù)據(jù)
5. 添加幾個(gè)靜態(tài)方法
修改student.js文件璧亚,在其中添加幾個(gè)靜態(tài)方法讨韭,這相當(dāng)于是直接在student架構(gòu)(類型)中直接添加方法,使用時(shí)不需要實(shí)例化對(duì)象,直接通過student來調(diào)用透硝。
//引包
var mongoose = require('mongoose');
var db = require('./db')
//創(chuàng)建一個(gè)學(xué)生架構(gòu)
var studentSchema = mongoose.Schema({
name: {
type: String,
default: '匿名用戶'
},
age: {
type: Number
},
sex: {
type: Boolean
}
})
//靜態(tài)方法是直接將方法創(chuàng)建到架構(gòu)中去狰闪,直接通過架構(gòu)打點(diǎn)調(diào)用
//添加
studentSchema.statics.addStudent=function(name,age,sex,callback){
this.model('Student').create({name:name,age:age,sex:sex},callback);
}
//創(chuàng)建一個(gè)查找功能
studentSchema.statics.findByName = function (name, callback) {
this.model('Student').find({
name: name
}, callback);
};
//更新
studentSchema.statics.updateInfoByQuery = function (query, data, callback) {
this.model('Student').updateMany(query, data, callback);
}
//刪除
studentSchema.statics.deleteByName = function (name, callback) {
this.model('Student').deleteMany({
name: name
}, callback);
}
//將學(xué)生架構(gòu)映射成一個(gè)學(xué)生(Student)模型
var studentModel = db.model('Student', studentSchema);
//將學(xué)生模型曝露出去
module.exports = studentModel;
使用它們,各種測(cè)試CRUD
var student = require('./model/student')
// //創(chuàng)建對(duì)象
// var admin= new student({
// name: 'admin',
// age: 33,
// sex: true
// });
// //保存
// admin.save(function () {
// console.log('ok');
// })
// //創(chuàng)建并保存另一個(gè)對(duì)象
// student.create({
// name: 'test',
// age: 18,
// sex: false
// })
// //通過姓名查找
// student.findByName('admin', function (err, result) {
// if (err)
// console.log(err);
// else
// console.log(result);
// })
// //更新
// student.updateInfoByQuery({
// name: 'admin'
// }, {
// $set: {
// age: 19
// }
// }, function (err, result) {
// if (err)
// console.log(err);
// else
// console.log(result);
// })
// //刪除
// student.deleteByName('admin',function(err,result){
// if (err)
// console.log(err);
// else
// console.log(result);
// })
// //添加
// student.addStudent('cc',66,true,function(err,result){
// if (err)
// console.log(err);
// else
// console.log(result);
// })
6. 總結(jié)一下
- 定義模型
var xxschema=mongoose.schema({}) - 靜態(tài)方法
xxschema.statics.方法名=function(callback){
}
其中4種常用操作
- this.model('模型名稱').create()
- this.model('模型名稱').find()
- this.model('模型名稱').updateMany()
- this.model('模型名稱').deleteMany()
- 創(chuàng)造模型
db.model('模型名稱',xxschema)