参考地址: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 多层模型的更多相关文章

  1. nodejs+mongoose+websocket搭建xxx聊天室

    简介 本文是由nodejs+mongoose+websocket打造的一个即时聊天系统:本来打算开发一个类似于网页QQ类似功能的聊天系统,但是目前只是开发了一个模块功能 --- 类似群聊的,即一对多的 ...

  2. mongoose populate

    mongoose具备关系数据库一样的关联查询,通过在schema模型中设置ref属性,然后在查询时使用populate关键字,可以达到关联查询的目的. 以下内容参考了mongoose官方文档http: ...

  3. nodejs+mongoose操作mongodb副本集实例

    继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目     express 项目名称 2:npm install mongoose    安装mongo ...

  4. Nodejs mongoose 详解

    前言 Mongoose 是在nodejs环境下,对mongodb进行便捷操作的对象模型工具.本文介绍解(翻)密(译)Mongoose插件. Schema 开始我们就要讲到Schema,一个Schema ...

  5. koa2+mongoose搭建框架模型

    由于学的是java,所以此框架多少有点java的影子,我觉得不必排斥语言,只要思想好,所有语言均可以通用.项目分以下几层 app.js项目启动入口,类似于main函数 controller-view层 ...

  6. Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose

    参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...

  7. mongoose基础使用

    mongoose与mongodb 首先,要明确mongoose和mongodb是什么? mongodb是一种文档数据库:而mongoose是一种能在node环境中优雅地操作mongodb的对象模型工具 ...

  8. 使用express4.X + jade + mongoose + underscore搭建个人电影网站

    (-。-;), 周末过得真是快啊,  很久以前就看到imooc上有个搭建个人电影网站一期 ,二期的视频, 这两周宅家里撸玩没事干, 我也学着搭了一个, 这些东西都是基础, 只要花点时间很好学的, no ...

  9. Mongoose:Schema之路

    说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. Mongoose学习 这里的Mongoose当然不是图片上的萌物,它是一个MongoDB对象建模工具(object model ...

随机推荐

  1. myapplication 单例写法

    MyApplication extends Application private static MyApplication myApplication = null; oncreate中: @Ove ...

  2. python 分布式进程体验

    抽了点时间体验了一把python 分布式进程,有点像分布式计算的意思,不过我现在还没有这个需求,先把简单体验的脚本发出来,供路过的各位高手指教 注:需要先下载multiprocessing 的pyth ...

  3. 分享自己新做的vim colorscheme

    把下面的内容保存成darkslategrey.vim,放入~/.vim/colors目录即可. " Vim color file " Maintainer: jiqing() &q ...

  4. 如何在python的字符串中输入纯粹的{}

    python的format函数通过{}来格式化字符串 >>> a='{0}'.format(123) >>> a ' 如果需要在文本中包含{}字符,这样使用就会报错 ...

  5. 动态获取UILabel的bounds

    在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度 CGSize labelBounds = [s ...

  6. 弹性盒模型justify-content属性

    justify-content是应用于父容器上来规定子元素在水平方向上的对齐方式的. flex-start 左对齐 flex-end 右对齐 center 居中 space-betten 两端对齐,两 ...

  7. python面向对象——类

    from:http://www.runoob.com/python3/python3-class.html Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在P ...

  8. 莫烦课程Batch Normalization 批标准化

    for i in range(N_HIDDEN): # build hidden layers and BN layers input_size = 1 if i == 0 else 10 fc = ...

  9. rsync本地及远程复制备份【原创】

    1.安装rsyncyum instsall rsync 2.本地复制 rsync -auq --progress --delete /tongbu1/ /tongbu2/ rsync -auq --p ...

  10. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...