mongoose.populate返回空数组 [英] mongoose.populate returning empty array

查看:72
本文介绍了mongoose.populate返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用猫鼬 v4.10.8 ,并尝试在已定义的模型上填充多个数组。

I am currently using mongoose v4.10.8 and am attempting to populate multiple arrays on a defined model.

TransportModel.js

//Transport model
'use strict';
const mongoose = require('mongoose');

const TransportSchema = new mongoose.Schema({
    tag: {
        type: String,
        enum: ['X', 'Y'],
        required: true,
        unique: true
    },
    chairs: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Chair"
    }],
    targets: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Target"
    }]
});

module.exports = mongoose.model('Transport', TransportSchema);

ChairModel.js

//Chair model
'use strict';
const mongoose = require('mongoose');

const ChairSchema = new mongoose.Schema({
    product_name: {
        type: String,
        required: true,
        unique: true
    },
    description: {
      type: String,
      unique: false,
      required: false
    }
});

module.exports = mongoose.model('Chair', ChairSchema);

TargetModel.js

//Target model
'use strict';
const mongoose = require('mongoose');

const TargetSchema = new mongoose.Schema({
    target_name: {
        type: String,
        required: true,
        unique: true
    },
    location: {
      type: String,
      required: true,
      unique: false
    },
    description: {
      type: String,
      unique: false,
      required: false
    }
});

module.exports = mongoose.model('Target', TargetSchema);

在完成上述配置后,我创建了一个 Chair 文档和一个 Target 文档。

After having the above configuration in place, I created one Chair document and one Target document.

此后,我在要获取的路线上调用以下逻辑单个 Transport 文档:

Afterwards, I invoke the following logic on a route to get a single Transport document:

TransportModel.findOne({
        tag: 'X'
    })
    .populate("chairs")
    .populate("targets")
    .exec(function(err, transport) {
        if (err) {
            console.error(err);
        }
        console.log('Transport: ', transport);
    });
});

以下是检索到的文档:

{
    "_id": "xxxx9999xxxx9999xxxx9999",
    "tag": "X",
    "__v": 1,
    "chairs": [],
    "targets": []
}

因此,即使我查询了 Chairs Targets 集合,并且确实有至少1个文档,即运输文档未填充。

Thus, even though I queried the Chairs and Targets collections and I do have at least 1 document, the Transport document does not get populated.

此外,我还尝试了 .populate()的增强版。 ,但我不断得到空数组:

Furthermore, I also tried the enhanced version of .populate(), but I keep getting empty arrays:

.populate({
  path: "chairs",
  model: "Chair"
})
.populate({
  path: "targets",
  model: "Target"
})


推荐答案

如果您已经将新的主席和目标推入了运输模型,您可以使用 mongoose-deep-populate 模块更好地填充它,下面是一个简单的示例,介绍了如何配置

If you already push the new chairs and targets into the Transport model, you can populate it better with the mongoose-deep-populate module, here is a simple example of how config to set it up.

var mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);

var Schema = mongoose.Schema;

var kindsSchema = new Schema({

    name:{type:String,required:true,unique:true},
    components:[{type:Schema.ObjectId,ref:'components'}],
    associatedView:{type:String,default:'home/fragances'},
    discount:{type:Number,default:0},
    description:{type:String,default:'Nueva categoria'},

    created_at:{type:Date,default:Date.now},
    modified_at:{type:Date,required:true}
});

kindsSchema.plugin(deepPopulate,{
  whitelist:[
    'components.attributes',
  ]
    populate:{
      'components':{
      select['attributes','discount']
      }
    }
});

https://www.npmjs.com/package/mongoose-deep-populate

这篇关于mongoose.populate返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆