nodejs mongoose populate 多层模型
参考地址:http://ronaldroe.com/populating-multiple-fields-and-levels-with-mongoose/

文字版本
Mongoose, the popular MongoDB library for NodeJS is incredibly robust and relatively easy to pick up. Its documentation, however leaves a little to be desired. To attain functionality similar to join functionality present in relational databases, Mongoose provides a method called populate(). Used in conjunction with the ref schema property, data can be pulled in from other documents in place of an id. The process is fairly straightforward. However, what Automattic’s documentation doesn’t explain is how to populate multiple paths, and the documentation for multiple levels leaves much to be desired. The documentation does point out that chaining multiple populate functions won’t work. How, then can this be done? Multiple paths may be populated by creating a space-separated list. Note the emphasis, because comma-separated lists do not work. By way of example: // Doesn't work - will only populate 'friends'
Users.findOne({/* query here */})
.populate('address')
.populate('friends'); // Works,多个关联字段
Users.findOne({/* query here */})
.populate('address friends');
Now, what if we wanted to populate the friends’ addresses? As explained in the docs, you would nest another populate by passing an object to the method, and adding another populate as a property. That was pretty confusing to me, but here’s what it means: // This time, we're not passing a string, but an object to populate(),多层关联
Users.findOne({/* query here */})
.populate({
path: 'address friends', // The string we passed in before
populate: {
path: 'address' // This will populate the friends' addresses
}
});
The first level of items to populate is passed in a key called path. The next level items to populate are passed as a new populate object. This can be continued on down the line as necessary. Each populate will have a path key/value pair, and if there’s another level of things to populate, those are passed in a populate key/value pair.
需求是因为有三个模型,工厂、车间、机器,关联关系是
工厂=》车间=》机器
即一个工厂对应多个车间,一个车间对应多个机器
所以在设计模型时,设计成,工厂模型、车间模型、机器模型,并在车间模型中保存了工厂模型的ID,也在机器模型中保存了车间模型的ID

对应的模拟代码是



那么对应的查询中,工厂只需要查询自己即可

车间需要关联至工厂,那么需要增加populate方法,对关联数据进行查询

当查询机器时,需要查询更多了

最终查询出来的

nodejs mongoose populate 多层模型的更多相关文章
- nodejs+mongoose+websocket搭建xxx聊天室
简介 本文是由nodejs+mongoose+websocket打造的一个即时聊天系统:本来打算开发一个类似于网页QQ类似功能的聊天系统,但是目前只是开发了一个模块功能 --- 类似群聊的,即一对多的 ...
- mongoose populate
mongoose具备关系数据库一样的关联查询,通过在schema模型中设置ref属性,然后在查询时使用populate关键字,可以达到关联查询的目的. 以下内容参考了mongoose官方文档http: ...
- nodejs+mongoose操作mongodb副本集实例
继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目 express 项目名称 2:npm install mongoose 安装mongo ...
- Nodejs mongoose 详解
前言 Mongoose 是在nodejs环境下,对mongodb进行便捷操作的对象模型工具.本文介绍解(翻)密(译)Mongoose插件. Schema 开始我们就要讲到Schema,一个Schema ...
- koa2+mongoose搭建框架模型
由于学的是java,所以此框架多少有点java的影子,我觉得不必排斥语言,只要思想好,所有语言均可以通用.项目分以下几层 app.js项目启动入口,类似于main函数 controller-view层 ...
- Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose
参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...
- mongoose基础使用
mongoose与mongodb 首先,要明确mongoose和mongodb是什么? mongodb是一种文档数据库:而mongoose是一种能在node环境中优雅地操作mongodb的对象模型工具 ...
- 使用express4.X + jade + mongoose + underscore搭建个人电影网站
(-。-;), 周末过得真是快啊, 很久以前就看到imooc上有个搭建个人电影网站一期 ,二期的视频, 这两周宅家里撸玩没事干, 我也学着搭了一个, 这些东西都是基础, 只要花点时间很好学的, no ...
- Mongoose:Schema之路
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. Mongoose学习 这里的Mongoose当然不是图片上的萌物,它是一个MongoDB对象建模工具(object model ...
随机推荐
- bat 批量更改文件名
通过如下批处理命令可实现批量更改文件名: @echo set DIR="%cd%" echo DIR=%DIR% set /p ext=文件类型: set /p find=要替换内 ...
- js小记 unicode 编码解析
var str = "\\u6211\\u662Funicode\\u7F16\\u7801"; // 关于这样的数据转换为中文问题,常用的两种方法. // 1. eval 解析 ...
- javascript iframe相关操作
1. 获得iframe的window对象 iframeElement.contentWindow 2. 获得iframe的document对象 存在跨域访问限制. chrome:iframeEleme ...
- [整理]C语言函数说明和定义
函数的一般形式是:type-specifier function_name(parameter list) parameter declarations{ body of the function ...
- Java开发者应该列入年度计划的5件事
本文写了我今年计划要做的5件事.为了能跟踪计划执行的进度,就把这些事都列了出来.我觉得这些事对其它Java开发者而言也是不错的参考方向. 1.开发一个应用,通过Java来操作一种NoSQL数据库实现存 ...
- Richard Stallman:让我们关注和尊敬自由软件教父
1953年,Richard Stallman生于美国纽约曼哈顿区.在度过了并不快乐的童年之后,他在哈佛大学找到了自己的家.在MIT人工智能实验室工作期间,展露出了自己的计算 机天赋.对他来说,开发操作 ...
- zedboard 初使用 -- 工具篇
<一> 安装ISE和Vivada: <二> 安装USB转UART驱动 <三> 安装USB转JTAG驱动插件 http://blog.sina.com.cn/s/bl ...
- td自动换行
自动换行方法: 1.在<td>中设置样式style为word-wrap:break-word;word-break:break-all; (一般情况只需要设置word-break:brea ...
- 南京邮电大学CTF密码学部分Writeup
异性相吸 1.xor 2.hex2binary 3.len(bin(miwen))==len(bin(mingwen)) # -*- coding:utf-8 -*- file_de = open(' ...
- Python的set集合详解
Python 还包含了一个数据类型 -- set (集合).集合是一个无序不重复元素的集.基本功能包括关系测试和消除重复元素.集合对象还支持 union(联合),intersection(交),dif ...