85.Mongoose指南 - Schema
转自:https://www.bbsmax.com/A/pRdBnKpPdn/
定义schema
用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构.
|
1
2
3
4
5
6
7
8
9
10
|
var mongoose = require('mongoose');var schema = mongoose.Schema;var blogSchema = new Schema({ titile: String, body: String, comments: [{body: String, date: Date}], date: {type: Date, default: Date.now}, hidden:Boolen}); |
创建model
格式是mongoose.model(modelName, schema);
|
1
|
var BlogModel = mongoose.model('Blog', blogSchema); |
实例化方法
model的实例是document. document有许多内置的实例方法. 我们可以为document定义自己的实例方法
|
1
2
3
4
5
6
|
var animalSchema = new Schema({name: String, type: String});//定义实例方法animalSchema.methods.findSimilarType = function(cb){ return this.model('Animal').find({type: this.type}, cb);} |
现在animal实例有findSimilarTypes方法了
|
1
2
3
4
5
6
|
var Animal = mongoose.model('Animal', animalSchema);var dog = new Animal({type: 'dog'});dog.findSimilarTypes(function(err, dogs){ console.log(dogs);}); |
Model静态方法
还可以给Model添加静态方法
|
1
2
3
4
5
6
7
8
9
10
|
animalSchema.statics.findByName = function(name, cb){ this.find({name: new RegExp(name, 'i')}, cb);}var Animal = mongoose.model('Animal', animalSchema);Animal.findByName('fido', function(err, animals){ console.log(animals);}); |
索引
索引分为field级别和schema级别. 如果使用复合索引那么必须使用schema索引
|
1
2
3
4
5
6
7
|
var animalSchema = new Schema({ name: String, type: String, tags: {type: [String], index:true} // field level});animalSchema.index({name:1, type:-1}); // schema level |
当应用启动的时候, mongoose会自动为你的schema调用ensureIndex确保生成索引. 开发环境用这个很好, 但是建议在生产环境不要使用这个.使用下面的方法禁用ensureIndex
|
1
2
3
|
animalSchema.set('autoIndex', false);//ornew Schema({}, {autoIndex: false}); |
Virtual
virtual是document的属性 你可以get,set他们但是不持续化到MongoDB. virtual属性get非常有用可以格式化或者合并字段, set可以分解一个字段到多个字段并持续化到数据库
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var personSchema = new Schema({ name: { first: String, last: String }});var Person = mongoose.model('Person', personSchema);var bad = new Person({ name: {first: 'Walter', last: 'White'}}); |
如果你想获取bad的全名 你需要这样做
|
1
|
console.log(bad.name.first + ' ' + bad.name.last); |
或者我们可以在personSchema中定义virtual getter. 这样我们就不需要在每个要用fullname的地方拼接字符串了
|
1
2
3
|
personSchema.virtual('name.full').get(function(){ return this.name.first + ' ' + this.name.last; ); |
现在我么可以使用 name.full虚属性了
|
1
|
console.log(bad.name.full); |
我们还可以通过设置this.name.full来设置this.name.first和this.name.last
|
1
|
bad.name.full = "Breaking Bad"; |
|
1
2
3
4
5
6
7
8
9
10
11
|
personSchema.virtual('name.full').set(function(name){ var split = name.split(' '); this.name.first = split[0]; this.name.last = split[1];});mad.name.full = "Breaking Bad";console.log(mad.name.first); // Breakingconsole.log(mad.name.last); // Bad |
Options
Schema有一些配置选项, 可以如下面一样设置
|
1
2
3
4
5
|
new Schema({}, options);//orvar xxSchema = new Schema({});xxSchema.set(option, value); |
option:autoIndex
应用启动的时候Mongoose会自动为每一个schema发送一个ensureIndex命令。 如果你想禁止自动创建index要自己手动来创建的话 你可以设置autoIndex为false
|
1
2
3
4
|
var xxSchema = new Schema({}, { autoIndex: false });var Clock = mongoose.model('Clock', xxSchema);Clock.ensureIndexs(callback); |
option:bufferCommands
todo
|
1
|
var schema = new Schema({}, { bufferCommands: false }); |
option:capped
todo
....
85.Mongoose指南 - Schema的更多相关文章
- [译]Mongoose指南 - Schema
定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...
- [译]Mongoose指南 - Population
MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...
- [译]Mongoose指南 - 验证
开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...
- [译]Mongoose指南 - Model
编译你的第一个model var xxSchema = new Schema({name: 'string', size: 'string'}); var Tank = mongoose.model( ...
- mongoose 建立schema 和model
在node中使用MongoDB很多情况下,都是使用mongoose的,所以这集来介绍一下 安装 yarn add mongoose 连接 const mongoose = require(" ...
- [译]Mongoose指南 - Connection
使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...
- [译]Mongoose指南 - Plugin
Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...
- [译]Mongoose指南 - 中间件
中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...
- [译]Mongoose指南 - 查询
查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...
随机推荐
- AssetBundle打包优化解决方式
第一阶段:AssetBundle出一套解决方式 1.解决如今同一个资源打2个bundle的冗余问题 2.測试验证节省资源的比率是多少 问题拆分 一.bundle反复 问 题 :同样资源拆分问题? ...
- Linux网络编程(3)——多进程、多线程
在我的里面已经介绍了linux以下c的进程.线程接口,这里就不做过多阐述了. 多进程 这里多进程採用传统的多进程模型.每当有client发来的连接时创建一个进程来处理连接,一个子进程相应一个连接. 有 ...
- kentico api
http://devnet.kentico.com/docs/10_0/api/html/R_Project_Kentico_API.htm ScriptHelper.RegisterClientSc ...
- js中callback执行
<!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title> ...
- 11.使用boostregex遭遇无法打开libboost_regex-vc120-mt-sgd-1_62.lib的问题
通过Boost库可以在C++项目中使用正则表达式,配置好环境后链接过程出现”无法打开libboost_regex-vc120-mt-sgd-1_62.lib”的错误. 原因是按照官方生成lib的方法 ...
- weboffice7
document.all.WebOffice1.ShowToolBar = false;
- 如何解决bib的一些问题
胡老师留的大作业要求综述,因而有很多文献引用.但是当使用bibtex的方法,特别是中文文献的引用会遇到一些问题. 网上相关的解答有: http://blog.sciencenet.cn/blog-10 ...
- select标签下option标签里value属性有什么用以及和text的区别
转自:http://blog.csdn.net/summer_sy/article/details/54572398 1:value的用处 <select > <option val ...
- 【原创】JMS发布者订阅者【异步接收消息】
发布订阅模式和PTP方式不同之处为后者依赖于一个Topic话题: package com.thunisoft.jms.mine.topic; import java.util.HashMap; imp ...
- Swift 4.0:访问级别(访问控制)
基础篇 注: 下文中所提及的类和类型为Class, Enum和Struct Swift中的访问级别有以下五种: open: 公开权限, 最高的权限, 可以被其他模块访问, 继承及复写. public: ...