-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
transform
option for lean()
#10423
Comments
Read this for more clarification |
@baohuynhlam we don't currently have support for transforms with Currently, I'd recommend structuring this as a post hook that applies your transform function if lean is set: schema.post(['find', 'findOne', 'findOneAndUpdate'], function(res) {
if (!this.mongooseOptions().lean) {
return;
}
if (Array.isArray(res)) {
res.forEach(transformDoc);
return;
}
transformDoc(res);
});
function transformDoc(doc) {
doc.id = doc._id;
delete doc._id;
delete doc.__v;
} |
transform
option for lean()
Would there be a way to do this after populating and also removing the _id and __v on populated documents @vkarpov15? It seems that with your current example (and expanding it to include sub-arrays with |
@mjfwebb I took a look and you're right, it isn't possible to do this with const parentSchema = new Schema({ name: String, children: [{ type: 'ObjectId', ref: 'Child' }] });
const childSchema = new Schema({ name: String });
parentSchema.post(['find', 'findOne', 'findOneAndUpdate'], function(res) {
if (!this.mongooseOptions().lean) {
return;
}
if (Array.isArray(res)) {
res.forEach(transformDoc);
return;
}
transformDoc(res);
});
function transformDoc(doc) {
if (doc._id != null) {
doc.id = doc._id;
delete doc._id;
}
delete doc.__v;
for (const key of Object.keys(doc)) {
if (doc[key] != null && doc[key].constructor.name === 'Object') {
transformDoc(doc[key]);
} else if (Array.isArray(doc[key])) {
for (const el of doc[key]) {
if (el != null && el.constructor.name === 'Object') {
transformDoc(el);
}
}
}
}
}
const Child = mongoose.model('Child', childSchema);
const Parent = mongoose.model('Parent', parentSchema);
const c = await Child.create({ name: 'test child' });
await Parent.create({ name: 'test parent', children: [c._id] });
const p = await Parent.findOne().lean().populate('children');
console.log(p); |
|
@vkarpov15 for our use case the solution above doesn't quite do it because
I'm up for writing a |
@rubenvereecken can you please provide some more concrete examples where the provided solution doesn't work? Here's where the current Lines 3587 to 3591 in 94d96b8
|
@vkarpov15 First issue was transform happening at the wrong time. For example this scenario:
The second problem is that your proposed solution's As I replied in another thread, our work-around is implementing our own recursive |
Gh 10423 adds a transform option to lean queries
Do you want to request a feature or report a bug?
Honestly, I don't know if this is a bug or a feature request
What is the current behavior?
When creating schema, I am using the
toObject
andtoJSON
options to strip fields like_id
and__v
before presenting it to the end-users. In some GET endpoints, I want to also uselean()
to increase the performance, as I am only returning the data, not actively modifying it (except fortoJSON
transformation). So far, I have had no success in making these features work together, and I am honestly not sure if these can work together or not, as thetoObject
doc does not talk aboutlean
and thelean
doc also does not really mentiontoObject
andtoJSON
If the current behavior is a bug, please provide the steps to reproduce.
My code:
What is the expected behavior?
I just want to ask how to make these feature work together, or is that an impossible thing? Are there any packages/plugins to help with my goal ?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: