You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the current behavior?
When using a model that has a document defintion with "type", mongoose cannot populate the document correctly.
If the current behavior is a bug, please provide the steps to reproduce.
const mongoose = require('mongoose');
console.log(mongoose.version);
run().catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test2');
// Broken - population fails with "Null", and it tries to populate via "ModelA" (NOT B)
const ModelA = mongoose.model('ModelA', new mongoose.Schema({
bla: {
select: false,
type: {
_reference: {
type: mongoose.Schema.Types.ObjectId,
ref: 'ModelB'
},
}
}
}));
// here everything works - same model, only difference is that "defintion" is not done via "custom type".
/* const ModelA = mongoose.model('ModelA', new mongoose.Schema({
bla: {
_reference: {
type: mongoose.Schema.Types.ObjectId,
ref: 'ModelB'
}
}
})); */
const ModelB = mongoose.model('ModelB', new mongoose.Schema({
name: { type: String }
}));
const b = new ModelB({name: 'test'});
await b.save();
const a = new ModelA({bla: {_reference: b._id}});
await a.save();
const result = await ModelA.find({}, '+bla')
.populate('bla._reference');
console.log('result', JSON.stringify(result, false, 3));
}
Mongoose doesn't support the syntax you're using. In the above example, Mongoose assumes that bla is mixed type. You need to define a separate schema for _reference, like type: new Schema({ _reference: ... })
I want to report a bug.
What is the current behavior?
When using a model that has a document defintion with "type", mongoose cannot populate the document correctly.
If the current behavior is a bug, please provide the steps to reproduce.
This results into:
What is the expected behavior?
Mongoose should also be able to populate documents when they are defined via "type".
Please mention your node.js, mongoose and MongoDB version.
mongoose 5.2.15, v8.11.4, mongodb 3.6
The text was updated successfully, but these errors were encountered: