var mongoose = require('mongoose');
<!-- var uri = 'mongodb://username:password@hostname:port/databasename'; -->
var uri = 'mongodb://xiaomo:xiaomo@115.29.137.34:27017/admin';
mongoose.connect(uri);
// model.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var BlogPost = new Schema({
author : ObjectId,
title : String,
body : String,
date : Date
});
//Model
mongoose.model('blog',Schema);
var Comment = new Schema({
name: { type: String, default: 'hahaha' },
age: { type: Number, min: 18, index: true },
bio: { type: String, match: /[a-z]/ },
date: { type: Date, default: Date.now }
});
// a setter
Comment.path('name').set(function (v) {
return capitalize(v);
});
// middleware
Comment.pre('save', function (next) {
notify(this.get('email'));
next();
});
4. 创建文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//insert.js
var mongoose = require('mongoose');
require('./model.js'); //执行
var Blog = mongoose.model('blog');
var blog = new Blog({
name:'xiaomo',
age:25,
bio:'don't know',
date:new Date
});
book.save(function(err){
console.log('save status:'+err);
});
5. 删除文档
1
2
3
4
5
6
7
8
9
10
11
12
var mongoose = require('mongoose');
require('./model.js');
var Blog = mongoose.model('blog');
Blog.findOne({'name':'xiaomo'},function(err,doc){
if(err){
console.log(err)
return;
}
if(doc){
doc.reomove();
}
})
6. 简单查询
1
2
3
4
5
6
7
8
9
10
var mongoose = require('mongoose');
require('./model.js');
var Blog = mongoose.model('blog');
Blog.find({},function(err,doc){
if(err){
console.log(err)
return;
}
console.log('result',doc)
})
第一个满足条件的结果
1
2
3
4
5
6
7
8
9
10
var mongoose = require('mongoose');
require('./model.js');
var Blog = mongoose.model('blog');
Blog.findOne({'name':'xiaomo'},function(err,doc){
if(err){
console.log(err)
return;
}
console.log('result',doc)
})
7. 条件语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// condation.js
var mongoose = require('mongoose');
require('./model.js');
var Blog = mongoose.model('blog');
//名字是xiaomo或者xiaoming 可以用$or和$and
var condation = {
$or $and:[
{'name':'xiaomo'},
{'name':'xiaoming'}
]
}
Blog.find(condation,function(err,doc){
if(err){
console.log(err)
return;
}
console.log('result',doc)
})
三、在项目中使用mongoose
1. 生成项目
koa koatest -e koatest
2. 安装依赖
cd koatest && npm install
3. 安装mongoose
npm install --save mongoose
4. 创建模型
1
2
3
4
5
6
7
8
9
// models/users.server.model.js
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
uid:Number,
username:String,
createTime:Date,
lastLogin:Date
})
module.exports = mongoose.model('User',UserSchema);
5. 创建配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
// config/config.js
module.exports={
mongodb:'mongodb://xiaomo:xiaomo@115.29.137.34:27017/admin'
}
//config/mongoose.js
var mongoose = require('mongoose');
var config = require('./config.js');
module.exports=function(){
var db = mongoose.connect(config.mongodb);
require('./../models/users.server.model.js');
return db;
}
6. 操作mongdodb 代码
1
2
3
4
5
6
7
8
9
10
11
12
//routers/users.js
var router = require('koa-router')();
var mongoose = require('mongoose');
var User = mongoose.model('User');
router.get('/', function *(next) {
//add
var user = new User({
uid:
})
this.body = yield User.find({});
});
module.exports = router;