在Mongoose中使用嵌套的populate处理数据

简介:

假设有如下mongodb的schema定义:

复制代码
drawApply = new Schema({
    salesId: { type: Schema.ObjectId, ref: 'sales' },
    money: Number,
    status: { type: Number, default: 0 },
    createTime: { type: Date, default: Date.now }
});

sales = new Schema({
    name: { type: String, required: true, unique: true },
    pwd: String,
    phone: String,
    merchant: { type: Schema.ObjectId, ref: 'merchant' },
    status: { type: Number, default: 0 }
});

merchant = new Schema({
    name: String,
    sname: String,
    type: String
});
复制代码

  表drawApply的salesId属性指定表sales的_id,表sales的属性merchant指定表merchant的_id。这是一种嵌套级联的关系。

  查找drawApply表的数据,并同时返回对应的sales表的数据,可以使用下面的方法:

drawApply.find().populate('salesId', '_id name phone merchant').sort({createTime: -1}).exec(function(err, list) {
  // list of drawApplies with salesIds populated
});

  返回的结果中除了drawApply表的数据外,还会包含salesId中_id,name,phone,merchant四个属性的值。但是merchant属性的值是以ObjectId的形式显示的,如果想知道对应的merchant其它属性的值,则需要使用到嵌套的populate。代码如下:

复制代码
drawApply.find().populate({
    path: 'salesId',
    select: '_id name phone merchant',
    model: 'sales',
    populate: {
        path: 'merchant',
        select: '_id sname',
        model: 'merchant'
    }).sort({createTime: -1}).exec(function(err, list) {
  // list of drawApplies with salesIds populated and merchant populated
});
复制代码

  如果drawApply表中还存在其它ObjectId类型的字段,则可以在populate方法后面继续跟其它的populate,使用方法相同,如:

复制代码
drawApply.find().populate({
    path: 'salesId',
    select: '_id name phone merchant',
    model: 'sales',
    populate: {
        path: 'merchant',
        select: '_id sname',
        model: 'merchant'
    })
    .populate('approver', 'name')
    .populate('operator', 'name')
    .sort({createTime: -1}).exec(function(err, list) {
  // list of drawApplies with salesIds populated and merchant populated
});
复制代码

  有关populate的具体用法可以参考mongoose的官方文档http://mongoosejs.com/docs/populate.html#deep-populate

 

本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/p/5192721.html,如需转载请自行联系原作者


相关文章
|
6月前
229Echarts - 3D 柱状图(Bar3D - Global Population)
229Echarts - 3D 柱状图(Bar3D - Global Population)
22 0
|
7月前
|
JavaScript IDE 开发工具
修改 Angular Component 构造函数参数被认为是 breaking change
修改 Angular Component 构造函数参数被认为是 breaking change
34 0
|
11月前
|
Linux 定位技术 数据安全/隐私保护
【Calculate】Calculate Linux安装操作记录
【Calculate】Calculate Linux安装操作记录
103 0
|
关系型数据库 MySQL 数据库
mysql数据库 isnull,ifnull,nullif的区别和用法
mysql数据库 isnull,ifnull,nullif的区别和用法
125 0
mysql数据库 isnull,ifnull,nullif的区别和用法
|
测试技术
std::accumulate的具体用法
std::accumulate的具体用法
319 0
std::accumulate的具体用法
|
编译器
为什么我们需要给 Angular library 创建多重入口 multiple entry point
为什么我们需要给 Angular library 创建多重入口 multiple entry point
214 0
为什么我们需要给 Angular library 创建多重入口 multiple entry point
|
存储 搜索推荐 API
使用 Angular Transfer State 的一个具体例子
使用 Angular Transfer State 的一个具体例子
156 0
使用 Angular Transfer State 的一个具体例子
Angular ctorParameters,decorator等元数据,包含在ActivatedRouteSnapshot数据结构里
Angular ctorParameters,decorator等元数据,包含在ActivatedRouteSnapshot数据结构里
91 0
Angular ctorParameters,decorator等元数据,包含在ActivatedRouteSnapshot数据结构里
Angular @NgModule providers里multi等于true在源代码里如何体现的
Angular @NgModule providers里multi等于true在源代码里如何体现的
91 0
Angular @NgModule providers里multi等于true在源代码里如何体现的
Angular NgModule providers字段维护了多个字段后的初始化实现
Angular NgModule providers字段维护了多个字段后的初始化实现
Angular NgModule providers字段维护了多个字段后的初始化实现