I am trying to use mongoose-deep-populate ( https://www.npmjs.com/package/mongoose-deep-populate ) and when I hit the code that invokes it, it only populates one level down
I have done :
npm install mongoose-deep-populate
npm reports back that it installed it:
C:\Users\Documents\GitHub\npm i mongoose-deep-populate
npm WARN uductions@0.0.1 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9
(node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for
fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current:
{"os":"win32","arch":"x64"})
+ mongoose-deep-populate@3.1.1
updated 1 package and audited 2727 packages in 9.753s
found 0 vulnerabilities
At the top of my API file, I have this:
const mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);
here is where I am trying to use deep populate:
Event.findOne({'_id':v.id}).deepPopulate('registration_REF registrations.incentives_REF')
.then(p =>{
return res.json(p);
})
.catch(p_e =>{
return res.status(400).json({error: 'unable populate ' + p_e});
})
SCHEMAS
Here is my Event schema (it is in it's respective file)
const EventSchema = new Schema({
registration_REF: { type: Schema.Types.ObjectId, ref: 'registrations' },
})
// this is required to get rid of exception
EventSchema.plugin(deepPopulate);
module.exports = Event = mongoose.model('events', EventSchema)
here is the registration schema: (it is in it's respective file)
const RegistrationsSchema = new Schema({
dateBeg: {type: Date, required: true},
dateEnd: {type: Date, required: true},
incentives_REF: [{ type: Schema.Types.ObjectId, ref: schemaNames.incentives, default: [] }],
})
module.exports = Registration = mongoose.model('registrations', RegistrationsSchema)
Here is incentives Schema: (it is in it's respective file)
const IncentiveSchema = new Schema({
notificationDays: {type: Number, required: true},
})
module.exports = Incentive = mongoose.model('incentives', IncentiveSchema)
OUTPUT
This is what I get for output when I run the code:
"_id": "5d40a40f93ba712bd43835c8",
"__v": 0,
"registration_REF": {
"incentives_REF": [
"5d406e9bc4a3d0110047dc82"
],
"_id": "5d406d237374b00aaca17fdc",
"dateBeg": "2018-11-29T00:00:00.000Z",
"dateEnd": "2019-11-29T00:00:00.000Z",
"__v": 10
}
所以事实证明,我只需要在包含引用中点“包括”该引用:
Event.findOne({'_id':v.id}).deepPopulate('registration_REF.incentives_REF')
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.