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 ...
随机推荐
- linux服务器上修改oracle数据库的字符集
linux服务器上以dba身份进入:sqlplus / as sysdba; 依次执行以下命令:shutdown immediate; startup mount; alter system enab ...
- 【c#】腾讯人脸识别api签名
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...
- 算法进阶之Leetcode刷题记录
目录 引言 题目 1.两数之和 题目 解题笔记 7.反转整数 题目 解题笔记 9.回文数 题目 解题笔记 13.罗马数字转整数 题目 解题笔记 14.最长公共前缀 题目 解题笔记 20.有效的括号 题 ...
- 3.fIddler的使用
https://blog.csdn.net/chaoyu168/article/details/51065644 https://blog.csdn.net/u013474436/article/de ...
- bzoj千题计划145:bzoj3262: 陌上花开
http://www.lydsy.com/JudgeOnline/problem.php?id=3262 三维偏序 第一维排序,第二维CDQ分治,第三维树状数组 #include<cstdio& ...
- Spring MVC表单防重复提交
利用Spring MVC的过滤器及token传递验证来实现表单防重复提交. 创建注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RU ...
- 【bzoj4942】[Noi2017]整数 压位+线段树
题目描述 P 博士将他的计算任务抽象为对一个整数的操作. 具体来说,有一个整数 $x$ ,一开始为0. 接下来有 $n$ 个操作,每个操作都是以下两种类型中的一种: 1 a b :将 $x$ 加上整数 ...
- 图片src拼接后台返回ID
本文地址:http://www.cnblogs.com/veinyin/p/8507403.html 在学习 CSS 时只了解了给固定地址,但是如果给的是一个需要拼接的地址就蒙了,以下是基于 Vue ...
- 反卷积Deconvolution
反卷积(转置卷积.空洞卷积(微步卷积))近几年用得较多,本篇博客主要是介绍一下反卷积,尤其是怎么计算反卷积(选择反卷积的相关参数) 图1 空洞卷积(微步卷积)的例子,其中下面的图是输入,上面的图是输出 ...
- php中各种hash算法的执行速度比较
更多内容推荐微信公众号,欢迎关注: PHP中的Hash函数很多,像MD4.MD5.SHA-1.SHA-256.SHA-384.SHA-512等我们比较常见,那么各个哈希的执行速度呢? $algos = ...