本文实例讲述了Node Mongoose用法。分享给大家供大家参考,具体如下:Mongoose简介是一个将JavaScript对象与数据库产生关系的一个框架,Object related model。操作对象,就是操作数据库了。对象产生了,同时也持久化(数据进入数据库)了。初步使用Mongoose连接数据库
var mongoose = require('mongoose');

//创建数据库连接
var db = mongoose.createConnection('mongodb://localhost:27017/zf');

//监听open事件
db.once('open',function ( callback ) {



console.log('数据库成功连接');


});

module.exports = db;

var mongoose = require('mongoose');

//创建数据库连接
var db = mongoose.createConnection('mongodb://localhost:27017/zf');

//监听open事件
db.once('open',function ( callback ) {



console.log('数据库成功连接');


});

module.exports = db;
定义模型创造schema -> 定义在schema上的scatic方法 -> 创造模型schema -> 定义在schema上的scatic方法 -> 创造模型
new mongoose.schema({}); //参数是json,定义字段。
new mongoose.schema({}); //参数是json,定义字段。创建模型 db.model(collectionsName,schemaName); db.model(collectionsName,schemaName);
var mongoose = require('mongoose');
var db = require('./db.js');

//创建一个schema结构。 schema--模式
var StudentSchema = new mongoose.Schema({

name: {type: String, default: '匿名用户'},

age: { type: Number },

sex: { type: String }
});
// 创建方法
StudentSchema.statics.zhaoren = function ( name,callback ) {

this.model('Student').find({'name': name},callback);
}

//创建修改方法
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;

var mongoose = require('mongoose');
var db = require('./db.js');

//创建一个schema结构。 schema--模式
var StudentSchema = new mongoose.Schema({

name: {type: String, default: '匿名用户'},

age: { type: Number },

sex: { type: String }
});
// 创建方法
StudentSchema.statics.zhaoren = function ( name,callback ) {

this.model('Student').find({'name': name},callback);
}

//创建修改方法
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 中只操作类,不操作数据库。
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的一个实例。 //不但创建的是猫的实例, find查询出来的也是猫的实例。

xiaomao.age = 10;

xiaomao.save();
})

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的一个实例。 //不但创建的是猫的实例, find查询出来的也是猫的实例。

xiaomao.age = 10;

xiaomao.save();
})
Schema定义文档结构支持的类型
String
Number
Date
Buffer
Boolean
Mixed
ObjectId
Array

String
Number
Date
Buffer
Boolean
Mixed
ObjectId
Array
定义对象(methods)方法实例出来的对象,使用的方法, 实例来调用。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mlln');
var db = mongoose.connection;

db.on('open',function ( callback ) {

console.log('数据库成功打开');
});

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 );

});
});

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mlln');
var db = mongoose.connection;

db.on('open',function ( callback ) {

console.log('数据库成功打开');
});

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文档操作构造函数构造函数, 参数1:集合名称, 参数2:Schema实例
db.model(“test1”, TestSchema );
db.model(“test1”, TestSchema );查询查询, 参数1忽略,或为空对象则返回所有集合文档
model.find({}, callback);
model.find({},field,callback);
//过滤查询,参数2: {‘name':1, ‘age':0} 查询文档的返回结果包含name , 不包含age.(_id默认是1)
model.find({},null,{limit:20});
//过滤查询,参数3: 游标操作 limit限制返回结果数量为20个,如不足20个则返回所有.
model.findOne({}, callback);
//查询找到的第一个文档
model.findById(‘obj._id', callback);
//查询找到的第一个文档,同上. 但是只接受 __id 的值查询

model.find({}, callback);
model.find({},field,callback);
//过滤查询,参数2: {‘name':1, ‘age':0} 查询文档的返回结果包含name , 不包含age.(_id默认是1)
model.find({},null,{limit:20});
//过滤查询,参数3: 游标操作 limit限制返回结果数量为20个,如不足20个则返回所有.
model.findOne({}, callback);
//查询找到的第一个文档
model.findById(‘obj._id', callback);
//查询找到的第一个文档,同上. 但是只接受 __id 的值查询
创建创建, 在集合中创建一个文档
Model.create(文档数据, callback))
Model.create(文档数据, callback))更新更新,参数1: 查询条件, 参数2: 更新对象,可以使用MondoDB的更新修改器
Model.update(conditions, update, function(error)
Model.update(conditions, update, function(error)删除删除, 参数1: 查询条件
Model.remove(conditions,callback);
Model.remove(conditions,callback); 希望本文所述对大家node.js程序设计有所帮助。