-
3
-
4
-
5
-
6
-
7
-
1
-
2
-
3
-
4
-
5
-
6
-
7
// 添加 mongoose 实例方法
mongooseSchema.methods.findbyusername = function(username, callback) {
return this.model('mongoose').find({username: username}, callback);
}
// 添加 mongoose 静态方法,静态方法在Model层就能使用
mongooseSchema.statics.findbytitle = function(title, callback) {
return this.model('mongoose').find({title: title}, callback);
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
相当于Schemal是一个构造函数,在这里可以添加方法findbyuseraname()和findbytitle
model结构
var mongooseModel = db.model('mongoose', mongooseSchema);
-
1
-
1
我理解为在test数据库下面创建了mongoose collection的连接
Entity结构
//增加操作
var doc = {username : 'emtity\_demo\_username', title : 'emtity\_demo\_title', content : 'emtity\_demo\_content'};
var mongooseEntity = new mongooseModel(doc);
mongooseEntity.save(function(error) {
if(error) {
console.log(error);
} else {
console.log('saved OK!');
}
// 关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
//查询操作
var mongooseEntity = new mongooseModel({});
mongooseEntity.findbyusername('model\_demo\_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
基于model的增加、修改与查询
// 增加记录 基于model操作
var doc = {username : 'model\_demo\_username', title : 'model\_demo\_title', content : 'model\_demo\_content'};
mongooseModel.create(doc, function(error){
if(error) {
console.log(error);
} else {
console.log('save ok');
}
// 关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
//修改记录
//mongooseModel.update(conditions, update, options, callback);
var conditions = {username : 'model\_demo\_username'};
var update = {$set : {age : 27, title : 'model\_demo\_title\_update'}};
var options = {upsert : true};
mongooseModel.update(conditions, update, options, function(error){
if(error) {
console.log(error);
} else {
console.log('update ok!');
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
// mongoose find
var criteria = {title : 'emtity\_demo\_title'}; // 查询条件
var fields = {title : 1, content : 1, time : 1}; // 待返回的字段
var options = {};
mongooseModel.find(criteria, fields, options, function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
// 删除记录
var conditions = {username: 'emtity\_demo\_username'};
mongooseModel.remove(conditions, function(error){
if(error) {
console.log(error);
} else {
console.log('delete ok!');
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
基于实例方法的查询
前面说到findbyusername()在schemal层定义,相当于实例化,然后继承findbyusername()方法
var mongooseEntity = new mongooseModel({});
mongooseEntity.findbyusername('model\_demo\_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
基于静态方法的查询
静态方法可以直接在模型层调用
// 基于静态方法的查询
mongooseModel.findbytitle('emtity\_demo\_title', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
最后
文章到这里就结束了,如果觉得对你有帮助可以点个赞哦
模型层调用
// 基于静态方法的查询
mongooseModel.findbytitle('emtity\_demo\_title', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
最后
文章到这里就结束了,如果觉得对你有帮助可以点个赞哦
[外链图片转存中…(img-nv1LW0KI-1718016158371)]