[译]Mongoose指南 - Population
MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了.
我们可以populate单个document, 多个document, plain object, multiple plain objects或query返回的全部object.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var personSchema = new Schema({
_id: Number,
name: String,
age: Number,
stories: [{type: Schema.Types.ObjectId, ref: 'story'}]
});
var storySchema = new Schema({
_creator: {type: Number, ref: 'Person'},
title: String,
fans: [{type: Number, ref: 'Person'}]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
目前为止我们创建了两个model. Person model有一个stories字段是以个ObjectId的集合. ref是告诉mongoose哪个model应该被popluate.
Story有一个_creator字段是Number类型的他是personSchema的Id
Saving ref
保存一个ref
var arron = new Person({_id:0, name: 'Aaron', age: 100});
arron.save(function(err){
if(err) return handleError(err);
var story1 = new Story({
titile: 'Once upon a timex',
_creator: arron._id //分配person的_id
});
story1.save(function(err){
if(err) return handleError(err);
});
});
Populate
填充story的_creator
Story
.findOne({title: 'Once upon a timex'})
.populate('_creator')
.exec(fucntion(err, story){
if(err) return handleError(err);
console.log(story._creator.name) // Aaron
});
Field selection
如果我们不想为story填充整个_creator 我们只想填充_creator的name怎么搞呢?
Story
.findOne({ title: /timex/i })
.populate('_creator', 'name') // only return the Persons name
.exec(function (err, story) {
if (err) return handleError(err); console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron" console.log('The creators age is %s', story._creator.age);
// prints "The creators age is null'
})
一次填充多个paths
如果我们想一次填充多个path怎么过搞呢
Story
.find(...)
.populate('fans author') // space delimited path names
.exec()
另一种写法
Story
.find(...)
.populate('fans')
.populate('author')
.exec()
按条件填充
Story
.find(...)
.populate({
path: 'fans',
match: { age: { $gte: 21 }},
select: 'name -_id',
options: { limit: 5 }
})
.exec()
[译]Mongoose指南 - Population的更多相关文章
- [译]Mongoose指南 - Schema
定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...
- [译]Mongoose指南 - Connection
使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...
- [译]Mongoose指南 - 验证
开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...
- [译]Mongoose指南 - 查询
查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...
- [译]Mongoose指南 - Model
编译你的第一个model var xxSchema = new Schema({name: 'string', size: 'string'}); var Tank = mongoose.model( ...
- [译]Mongoose指南 - Plugin
Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...
- [译]Mongoose指南 - 中间件
中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...
- [译]Mongoose指南 - Document
更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...
- (译)快速指南:用UIViewPropertyAnimator做动画
翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...
随机推荐
- [资料搜集狂]D3.js数据可视化开发库
偶然看到一个强大的D3.js,存档之. D3.js 是近年来十分流行的一个数据可视化开发库. 采用BSD协议 源码:https://github.com/mbostock/d3 官网:http://d ...
- POJ #2448 A New Operating System
Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 1165 Accepted: 110 Case Time Limit: ...
- Pythonn new-style class and old-style class
In [1]: class old(): ...: a = 1 ...: In [2]: o = old() In [3]: o.__class__ Out[3]: <class __main_ ...
- 修改MySQL的默认数据存储引擎
因为MySQL默认的是MyISAM数据引擎,不支持事务也不支持外键,所以需要用到Innodb引擎,于是决定将mysql的默认引擎设置为innodb.1 . 查看MySQL存储引擎是用的哪个?登录MyS ...
- python BeautifulSoup4
source form http://www.bkjia.com/ASPjc/908009.html 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Py ...
- markdown安装和使用
下载 运行markdownpad2-setup.exe文件,下一步一直到结束. 使用 标题 列表 引用.网页链接.图片链接 代码框 星号
- omnet++5.0安装使用
1.下载Windows安装包,5.0的omnetpp-5.0-src-windows.zip 2.解压到d盘 3.D:\omnetpp-5.0\doc找到这个目录,下面有个InstallGuide.p ...
- SQL Server编程(06)触发器
SQL Server 通过触发器用来保证业务逻辑和数据的完整性.在SQL Server中,触发器是一种特殊类型的存储过程,可在执行语言事件时自动触发.SQL Server中触发器包括三种:DML触发器 ...
- SQLServer------如何删除列为NULL的行
delete from WeiXinInfo where ManagerID IS NULL;
- mysql-python
sudo pip install MySQL-python centos安装 python-dev包提示No package python-dev available: 出现此问题的原因是python ...