Node Mongoose用法詳解【Mongoose使用、Schema、對象、model文檔等】
本文實例講述了Node Mongoose用法。分享給大家供大家參考,具體如下:
Mongoose簡介
是一個將JavaScript對象與數(shù)據(jù)庫產(chǎn)生關系的一個框架,Object related model。操作對象,就是操作數(shù)據(jù)庫了。對象產(chǎn)生了,同時也持久化(數(shù)據(jù)進入數(shù)據(jù)庫)了。
初步使用Mongoose
連接數(shù)據(jù)庫
var mongoose = require('mongoose');
//創(chuàng)建數(shù)據(jù)庫連接
var db = mongoose.createConnection('mongodb://localhost:27017/zf');
//監(jiān)聽open事件
db.once('open',function ( callback ) {
console.log('數(shù)據(jù)庫成功連接');
});
module.exports = db;
定義模型
創(chuàng)造schema -> 定義在schema上的scatic方法 -> 創(chuàng)造模型
new mongoose.schema({}); //參數(shù)是json,定義字段。
創(chuàng)建模型 db.model(collectionsName,schemaName);
var mongoose = require('mongoose');
var db = require('./db.js');
//創(chuàng)建一個schema結(jié)構(gòu)。 schema--模式
var StudentSchema = new mongoose.Schema({
name: {type: String, default: '匿名用戶'},
age: { type: Number },
sex: { type: String }
});
// 創(chuàng)建方法
StudentSchema.statics.zhaoren = function ( name,callback ) {
this.model('Student').find({'name': name},callback);
}
//創(chuàng)建修改方法
StudentSchema.statics.xiugai = function ( conditions,update,options,callback ) {
this.model('Student').update(conditions,update,options,callback);
}
var studentModel = db.model('Student',StudentSchema);
module.exports = studentModel;
app.js 中只操作類,不操作數(shù)據(jù)庫。
var Cat = mongoose.model('Cat'{'name': String, age: Number});
Cat.find({'name': 'tom'},function( err.reslut ){
var xiaomao = reslut[0];
//小貓這個變量是一個Cat的實例,它是從Cat集合中find出來的,所以find出來以后,就是Cat的一個實例。 //不但創(chuàng)建的是貓的實例, find查詢出來的也是貓的實例。
xiaomao.age = 10;
xiaomao.save();
})
Schema
定義文檔結(jié)構(gòu)支持的類型
String Number Date Buffer Boolean Mixed ObjectId Array
定義對象(methods)方法
實例出來的對象,使用的方法, 實例來調(diào)用。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mlln');
var db = mongoose.connection;
db.on('open',function ( callback ) {
console.log('數(shù)據(jù)庫成功打開');
});
var animalSchema = new mongoose.Schema({
'name': String,
'type': String
});
animalSchema.methods.zhaotonglei = function ( cb ) {
this.model('Animal').find({'type': this.type},cb);
}
var Animal = mongoose.model('Animal',animalSchema);
//module.exports = Blog;
/*Animal.create({'name': '湯姆','type': '貓'});
Animal.create({'name': 'imim','type': '貓'});
Animal.create({'name': '小白','type': '狗'});
Animal.create({'name': '加菲貓','type': '貓'});
Animal.create({'name': 'snoopy','type': '狗'});
*/
//blog.save();
Animal.findOne({'name': 'imim'},function ( err,reslut ) {
var dog = reslut;
dog.zhaotonglei(function ( err,resluts ) {
console.log( resluts );
});
});
model文檔操作
構(gòu)造函數(shù)
構(gòu)造函數(shù), 參數(shù)1:集合名稱, 參數(shù)2:Schema實例
db.model(“test1”, TestSchema );
查詢
查詢, 參數(shù)1忽略,或為空對象則返回所有集合文檔
model.find({}, callback);
model.find({},field,callback);
//過濾查詢,參數(shù)2: {‘name':1, ‘a(chǎn)ge':0} 查詢文檔的返回結(jié)果包含name , 不包含age.(_id默認是1)
model.find({},null,{limit:20});
//過濾查詢,參數(shù)3: 游標操作 limit限制返回結(jié)果數(shù)量為20個,如不足20個則返回所有.
model.findOne({}, callback);
//查詢找到的第一個文檔
model.findById(‘obj._id', callback);
//查詢找到的第一個文檔,同上. 但是只接受 __id 的值查詢
創(chuàng)建
創(chuàng)建, 在集合中創(chuàng)建一個文檔
Model.create(文檔數(shù)據(jù), callback))
更新
更新,參數(shù)1: 查詢條件, 參數(shù)2: 更新對象,可以使用MondoDB的更新修改器
Model.update(conditions, update, function(error)
刪除
刪除, 參數(shù)1: 查詢條件
Model.remove(conditions,callback);
希望本文所述對大家node.js程序設計有所幫助。
相關文章
node跨域轉(zhuǎn)發(fā) express+http-proxy-middleware的使用
這篇文章主要介紹了node跨域轉(zhuǎn)發(fā) express+http-proxy-middleware的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

