Schema.options.toObject.transform runs for every findOne query
See original GitHub issueI am running “mongoose@6.0.9” and Node@14.17.6
Do you want to request a feature or report a bug?
I believe it is a bug given that this behaviour wasn’t present in 5.11.11
What is the current behavior?
When creating schema, I am using the toObject.transform
options to strip fields like __v before presenting to users. I have noticed that whenever I set a toObject.transform
function, it runs in all findOne
queries
If the current behavior is a bug, please provide the steps to reproduce.
My code:
const schema = new mongoose.Schema({
name: String,
place: String
});
if (!schema.options.toObject) schema.options.toObject = {};
schema.options.toObject.transform = function (doc, ret, options) {
console.log('ret:', ret)
delete ret.__v;
delete ret.place;
return ret;
}
const model = mongoose.model("Model", schema);
async function findDocument() {
await model.create({name: "Abel", place:"Istanbul"})
const test= await model.findOne({name: "Abel"});
console.log(test) // returns object with _id and name. I need to prevent transform from running
}
With the transform function commented out, findOne query returns a Mongo document complete with fields
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Mongoose v6.8.2: API docs
SchemaTypeOptions (). The constructor used for schematype options ... An array containing all connections associated with this Mongoose instance.
Read more >Mongoose toObject and toJSON transform behavior with sub ...
Mongoose supports two Schema options to transform Objects after querying MongoDb: toObject and toJSON . In general you can access the returned ...
Read more >How to protect the password field in Mongoose/MongoDB so it ...
JohnnyHKs answer using Schema options is probably the way to go here. ... findOne() // This should return an object excluding the password...
Read more >An 80/20 Guide to Mongoose Plugins - The Code Barbarian
The last plugin use case you'll see in this article is tweaking schema options. This use case is primary useful for defining transform...
Read more >Mongoose API v4.4.14
Equivalent to calling .plugin(fn) on each Schema you create. ... Mongoose#Query() ... transform : optional function which accepts a mongoose document.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your transform does not apply to the underlying document, only to method calls targeting .toObject() or .toJSON(). Logging the top-level object applies the transform, but logging a property does not.
It is likely that logging a document calls toObject under the hood, but I haven’t checked the source code to point out exactly where.
@deistermatheus is right,
console.log(test)
calls the transform implicitly, here’s the relevant code in Mongoose.console.log()
callsrequire('util').inspect()
, which looks for theinspect()
function. So this is expected behavior.transform
is not called onfindOne()
.