参考地址: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. hbase系列之:独立模式部署hbase

    一.概述 在上一篇博文中,我简要介绍了hbase的部分基础概念,如果想初步了解hbase的理论,可以参看上一篇博文 hbase系列之:初识hbase .本博文主要介绍独立模式下部署hbase及hbas ...

  2. Redis学习十一:Redis的Java客户端Jedis

    一.安装JDK tar -zxvf jdk-7u67-linux-i586.tar.gz vi /etc/profile 重启一次Centos 编码验证 二.安装eclipse 三.Jedis所需要的 ...

  3. Kafka消息系统基础知识索引

    一些观念的修正 从 0.9 版本开始,Kafka 的标语已经从“一个高吞吐量,分布式的消息系统”改为"一个分布式流平台". Kafka不仅仅是一个队列,而且是一个存储,有超强的堆积 ...

  4. Python练习-跨目录调用模块

    层级结构: dir1 ---hello.py dir2 ---main.py 其中,hello.py: def add(x,y): return x+y main.py如何能调用到hello.py中的 ...

  5. 差分约束系统+spfa(B - World Exhibition HDU - 3592 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...

  6. [转]Linux 线程分离状态

    线程的分离与结合 在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死:在被其他线程回收之前,它的存储器资源(如栈) ...

  7. Python标准库内置函数complex介绍

    from:http://www.jb51.net/article/57798.htm 本函数可以使用参数real + imag*j方式创建一个复数.也可以转换一个字符串的数字为复数:或者转换一个数字为 ...

  8. 【CTF MISC】文件内容反转方法-2017世安杯CTF writeup详解

    Reverseme 用winhex打开,发现里面的字符反过来可以正常阅读,所以文件被倒置了 Python解题程序如下 with open('reverseMe','rb') as f: with op ...

  9. Android Framebuffer介绍及使用【转】

    转自:https://www.jianshu.com/p/df1213e5a0ed 来自: Android技术特工队 作者: Aaron 主页: http://www.wxtlife.com/ 原文连 ...

  10. 源码安装postgresql数据库

    一般情况下,postgresql由非root用户启动. 1.创建postgres用户 groupadd postgres useradd -g postgres postgres 下面的操作都在pos ...