mongoose 操作数据库1
来源:互联网 发布:打电话变女声软件 编辑:程序博客网 时间:2024/04/29 13:09
Mongoose 的几个重要的概念:
(1)连接(2)模型Schema(3)索引(4)分页(5)转换(6)中间件(7)自动产生键
通用操作:
var mongoose = require('mongoose')//获取mongoose模块
var Schema = mongoose.Schema;//获取Schema模块
//创建模型,这个模型指出了数据库中集合的字段类型
var CharacterSchema = Schema({
name: { type: String, required: true }
, health: { type: Number, min: 0, max: 100 }
})
有了模型必须要把模型添加到mongoose的模型中:
var Character = mongoose.model('Character', CharacterSchema);
这个方法有是三个参数,第一个是获取模型的名称,第二个是Schema的对象,第三个是数据库中集合名称,如果不指定第三个参数,那么数据库中集合的名称就使用第一个参数的附属形式。
在mongoose中操作数据库都是通过模型,来操作的。
(一)首先介绍保存的方法:
create:方法
Character.create({ name: 'Link', health: 100 }, function (err, link) {
if (err) return done(err);
console.log('found', link);
link.attack(); // 'Link is attacking'
mongoose.disconnect();
// done();
})
save方法
new Character({ name: 'Link', health: 100 }).save( function (err, link) {
if (err) return done(err);
console.log('found', link);
mongoose.disconnect();
});
给Schema 添加自定义方法:
CharacterSchema.methods.show = function () {
console.log('%s is attacking', this.name);
}
这个方法写在var Character = mongoose.model('Character', CharacterSchema,'Character');的前面。
(二)Schema
var CharacterSchema = Schema({
name: { type: String, required: true }
, health: { type: Number, min: 0, max: 100 }
})
这个也可以这么写:
var CharacterSchema = Schema({
name:String
, health: Number
})
字段类型:
{ type: String, required: true }字符串类型,必须
{ type: String, index: true } 字符串类型,索引
Date 日期类型
如果有多级目录,那么可以在comments中
var Comment = new Schema();
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
var Comment = new Schema();
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
var BlogPost = new Schema({
title : { type: String, index: true }//主键索引
, slug : { type: String, lowercase: true, trim: true }//大写
, date : Date //日期
, buf : Buffer //图片
, comments : [Comment]
, creator : Schema.ObjectId //ID
});
var Comment = new Schema();
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
{ type: String, required: true, index: { unique: true, sparse: true } }
var Comment = new Schema();
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
这是查询:
// when querying data, instead of providing a callback, you can instead
// leave that off and get a query object returned
var query = Person.find({ age : { $lt : 1000 }});
// this allows you to continue applying modifiers to it
query.sort('birthday');
query.select('name');
// you can chain them together as well
// a full list of methods can be found:
// http://mongoosejs.com/docs/api.html#query-js
query.where('age').gt(21);
// finally, when ready to execute the query, call the exec() function
query.exec(function (err, results) {
if (err) throw err;
console.log(results);
cleanup();
});
Comment.add({
title : { type: String, index: true }
, date : Date
, body : String
, comments : [Comment]
});
- mongoose 操作数据库1
- mongoose 数据库操作2
- mongoose 数据库操作3
- mongoose 数据库操作 - 分页
- mongoose 操作mongodb数据库的
- nodejs操作mongodb数据库(mongoose)
- nodejs mongoose 操作mongodb 数据库封装
- 利用mongoose操作mongodb数据库(一)
- Mongoose 操作
- Mongoose – 让NodeJS更容易操作Mongodb数据库
- Mongoose - 让NodeJS更容易操作Mongodb数据库
- mongodb查询语句及mongoose操作数据库总结
- 在koa中使用mongoose操作mongodb数据库
- mongodb & mongoose 常用操作
- Nodejs mongoose操作mongodb
- mongoose时间区间操作
- mongodb操作之mongoose
- Mongoose使用操作
- 如何添加网站for Linux(绑定域名)
- Vmware虚拟机中Linux虚拟机分辨率更改
- SVG动画 - 跳舞的女郎
- 电脑如何做服务器,面对动态IP和NAT转发后无公网IP,一样发布应用
- 网站权重与搜索排名的关系
- mongoose 操作数据库1
- 程序员永远的痛之字符编码的奥秘
- 【分治】UVa 11129 An antiarithmetic permutation
- 西邮Linux兴趣小组13级纳新免试题浅析(上)
- asp.net,copy网页的代码去掉格式化代码前面的序号
- 小米路由为何要搭载搜狗预取引擎
- Find uptime of 'apache2'
- 安全图
- Floyd最短路径(多源最短路径)