-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Propagate toObject
and toJSON
options to implicitly created schemas
#13325
Comments
This bug is also present on mongoose 7. What's interesting is that although it won't print to the console when you view the entire doc, the property is still there when you attempt to access that specific property. const mongoose = require('mongoose');
const userSchema = new mongoose.Schema(
{
firstName: String,
companies: {
type: [
{
companyId: {
type: mongoose.Schema.Types.ObjectId,
},
companyName: String,
},
],
required: true,
},
},
{
toObject: {
virtuals: true,
},
toJSON: { virtuals: true }
}
);
// const userSchema = new mongoose.Schema(
// {
// firstName: String,
// email: String,
// companies: {
// type: [{}],
// required: true,
// },
// },
// {
// toObject: {
// virtuals: true,
// },
// }
// );
userSchema.virtual("companies.details", {
ref: "company",
localField: "companies.companyId",
foreignField: "_id",
justOne: true,
});
userSchema.set("collection", "users");
const User = mongoose.model("user", userSchema);
const companySchema = new mongoose.Schema({
name: {
type: String,
},
legalName: {
type: String,
required: true,
},
});
companySchema.set("collection", "companies");
const Company = mongoose.model("company", companySchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const company = await Company.create({
name: 'Google',
legalName: 'Alphabet Inc'
});
const user = await User.create({
firstName: 'Test',
companies: [{ companyId: company._id, companyName: 'Google' }]
});
const test = await User.findOne().populate('companies.details');
console.log('what is test', test, test.companies[0].details);
}
run(); |
@IslandRhythms that's because you're missing a |
But its set in the schema? Doesn't that remove the need to include that in the consolel log? |
Good point. |
toObject
and toJSON
options to implicitly created schemas
enhancement: propagate `toObject` and `toJSON`
should there also be a way to pass down options for |
@hasezoey 2 ideas come to mind:
|
i think it would be possible, but was asking about a general option so that when calling |
AFAIK that's not a problem, |
Discussed in #13303
Originally posted by skrishnan22 April 20, 2023
These are my schemas
user.js
company.js
On running
in mongoose 5, companies.details is being populated correctly, but in mongoose 6 the populate is not happening
The populate works in mongoose 6, if I change the User schema as below. Note the change to companies field
Is this expected change when migrating from 5 to 6 and if so what is the best way to make it work with versions 6 and above
The text was updated successfully, but these errors were encountered: