node.js下mongoose简单操作实例

Mongoose API : http://mongoosejs.com/docs/api.html

// mongoose 链接

1
2
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS');

1
2
3
db.on('error', function(error) {
console.log(error);
});

// Schema 结构

1
2
3
4
5
6
7
var mongooseSchema = new mongoose.Schema({
username : {type : String, default : '匿名用户'},
title : {type : String},
content : {type : String},
time : {type : Date, default: Date.now},
age : {type : Number}
});

// 添加 mongoose 实例方法

1
2
3
mongooseSchema.methods.findbyusername = function(username, callback) {
return this.model('mongoose').find({username: username}, callback);
}

1
2
3
mongooseSchema.statics.findbytitle = function(title, callback) {
return this.model('mongoose').find({title: title}, callback);
}

// model

1
var mongooseModel = db.model('mongoose', mongooseSchema);

// 增加记录 基于 entity 操作

1
2
3
4
5
6
7
8
9
10
11
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();
});

// 增加记录 基于model操作

1
2
3
4
5
6
7
8
9
10
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
12
13
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
// 基于实例方法的查询
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
mongooseModel.findbytitle('emtity_demo_title', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});

// mongoose find

1
2
3
4
5
6
7
8
9
10
11
12
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
var conditions = {username: 'emtity_demo_username'};
mongooseModel.remove(conditions, function(error){
if(error) {
console.log(error);
} else {
console.log('delete ok!');
}

//关闭数据库链接
db.close();
});

坚持原创技术分享,您的支持将鼓励我继续创作!