Any ideas, contributions, improvements for MONGOOSE-LEAN-EXTENSION plugin #15460
Replies: 3 comments
|
Cool, thanks for writing this plugin 👍 For |
|
Thanks, let me update that
…On Wed, Jun 4, 2025 at 7:29 PM Valeri Karpov ***@***.***> wrote:
Cool, thanks for writing this plugin 👍
For record._id = record?._id?.toString();, I'd advise against doing that
because _id can be any type. You should check whether _id is an ObjectId
in the schema, otherwise you might end up converting numbers to strings.
—
Reply to this email directly, view it on GitHub
<#15460 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BJDTIAWTEM32YDFR3HYQHMT3B4NGHAVCNFSM6AAAAAB6P4UJW6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTGMZXGAYDSNQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
The plugin addresses real pain points with .lean() results. A few thoughts on the approach and how to handle the same needs without an extra dependency. For ObjectId stringification without a plugin: const doc = await MyModel.findById(id).lean() For __v removal you can use the projection: const doc = await MyModel.findById(id).select("-__v").lean() Or configure it schema-wide: const schema = new Schema({ ... }, { versionKey: false }) The transform option in toJSON covers the JSON serialization case but lean() bypasses toJSON entirely, which is why plugins like yours exist. The plugin approach is valid when you need a consistent behavior across many models without modifying each query individually. The main tradeoff is that it adds a dependency and must be updated when Mongoose changes its lean internals. |
Uh oh!
There was an error while loading. Please reload this page.
MONGOOSE-LEAN-EXTENSION plugin
Why is this mongoose plugin written??
Your mongoose query has .lean() but;
Why should you use it?
The plugins extends the .lean() functionality to;
mongoose.plugin(require("mongoose-lean-extension")), even populate results will include stringified_idAll reactions