简体   繁体   中英

mongoose.populate returning empty array

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);

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

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);
    });
});

Below is the retrieved document:

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

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.

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"
})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM