Mongoose API :
http://mongoosejs.com/docs/api.html
1 | // mongoose 链接 |
2 | var mongoose = require( 'mongoose' ); |
3 | var db = mongoose.createConnection( 'mongodb://127.0.0.1:27017/NodeJS' ); |
1 | // 链接错误 |
2 | db.on( 'error' , function (error) { |
3 | console.log(error); |
4 | }); |
1 | // Schema 结构 |
2 | var mongooseSchema = new mongoose.Schema({ |
3 | username : {type : String, default : '匿名用户' }, |
4 | title : {type : String}, |
5 | content : {type : String}, |
6 | time : {type : Date, default : Date.now}, |
7 | age : {type : Number} |
8 | }); |
1 | // 添加 mongoose 实例方法 |
2 | mongooseSchema.methods.findbyusername = function (username, callback) { |
3 | return this .model( 'mongoose' ).find({username: username}, callback); |
4 | } |
1 | // 添加 mongoose 静态方法,静态方法在Model层就能使用 |
2 | mongooseSchema.statics.findbytitle = function (title, callback) { |
3 | return this .model( 'mongoose' ).find({title: title}, callback); |
4 | } |
1 | // model |
2 | var mongooseModel = db.model( 'mongoose' , mongooseSchema); |
01 | // 增加记录 基于 entity 操作 |
02 | var doc = {username : 'emtity_demo_username' , title : 'emtity_demo_title' , content : 'emtity_demo_content' }; |
03 | var mongooseEntity = new mongooseModel(doc); |
04 | mongooseEntity.save( function (error) { |
05 | if (error) { |
06 | console.log(error); |
07 | } else { |
08 | console.log( 'saved OK!' ); |
09 | } |
10 | // 关闭数据库链接 |
11 | db.close(); |
12 | }); |
01 | // 增加记录 基于model操作 |
02 | var doc = {username : 'model_demo_username' , title : 'model_demo_title' , content : 'model_demo_content' }; |
03 | mongooseModel.create(doc, function (error){ |
04 | if (error) { |
05 | console.log(error); |
06 | } else { |
07 | console.log( 'save ok' ); |
08 | } |
09 | // 关闭数据库链接 |
10 | db.close(); |
11 | }); |
01 | // 修改记录 |
02 | mongooseModel.update(conditions, update, options, callback); |
03 | var conditions = {username : 'model_demo_username' }; |
04 | var update = {$set : {age : 27, title : 'model_demo_title_update' }}; |
05 | var options = {upsert : true }; |
06 | mongooseModel.update(conditions, update, options, function (error){ |
07 | if (error) { |
08 | console.log(error); |
09 | } else { |
10 | console.log( 'update ok!' ); |
11 | } |
12 | //关闭数据库链接 |
13 | db.close(); |
14 | }); |
01 | // 查询 |
02 | // 基于实例方法的查询 |
03 | var mongooseEntity = new mongooseModel({}); |
04 | mongooseEntity.findbyusername( 'model_demo_username' , function (error, result){ |
05 | if (error) { |
06 | console.log(error); |
07 | } else { |
08 | console.log(result); |
09 | } |
10 | //关闭数据库链接 |
11 | db.close(); |
12 | }); |
01 | // 基于静态方法的查询 |
02 | mongooseModel.findbytitle( 'emtity_demo_title' , function (error, result){ |
03 | if (error) { |
04 | console.log(error); |
05 | } else { |
06 | console.log(result); |
07 | } |
08 | //关闭数据库链接 |
09 | db.close(); |
10 | }); |
01 | // mongoose find |
02 | var criteria = {title : 'emtity_demo_title' }; // 查询条件 |
03 | var fields = {title : 1, content : 1, time : 1}; // 待返回的字段 |
04 | var options = {}; |
05 | mongooseModel.find(criteria, fields, options, function (error, result){ |
06 | if (error) { |
07 | console.log(error); |
08 | } else { |
09 | console.log(result); |
10 | } |
11 | //关闭数据库链接 |
12 | db.close(); |
13 | }); |
01 | // 删除记录 |
02 | var conditions = {username: 'emtity_demo_username' }; |
03 | mongooseModel.remove(conditions, function (error){ |
04 | if (error) { |
05 | console.log(error); |
06 | } else { |
07 | console.log( 'delete ok!' ); |
08 | } |
09 |
10 |
11 | //关闭数据库链接 |
12 | db.close(); |
13 | }); |