Query 'lean' option doesn't produce POJOs
See original GitHub issueThe Query docs state that when using lean
“documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments.”. However, the fact that the _id
key of the document is an object
, not a string
contradicts this indicated POJO nature. Surely the _id
key should be a string when converting to a plain javascript object.
Currently, checking equality requires a toString
call on any _id
s, for example when an _id
has been supplied by http request and needs to be compared to an _id
of a lean
query result.
Is there a way to ensure _id
is a string
, not an object
?
const Promise = require('bluebird');
const mongoose = require('mongoose')
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/test');
const personSchema = mongoose.Schema({ name : String });
const Person = mongoose.model('Person', personSchema);
const p = new Person({ name : 'Turing' });
return p.save().then(() => {
Person.findOne({ name : 'Turing' }).then((doc) => {
console.log('non-lean _id type', typeof doc._id, doc._id)
});
Person.findOne({ name : 'Turing' }).lean(true).then((doc) => {
console.log(' lean _id type', typeof doc._id, doc._id)
});
});
Produces output:
non-lean _id type object 57c38e4a01456bbc213b7ff2
lean _id type object 57c38e4a01456bbc213b7ff2
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:5
Top Results From Across the Web
Faster Mongoose Queries With Lean
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents...
Read more >Make mongoose lean() populated queries return POJOs ...
The populate function in Mongoose https://mongoosejs.com/docs/api/query.html#query_Query-populate has an option clone:true.
Read more >Faster Mongoose Queries With Lean - 书栈网
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result ...
Read more >How to optimize MongoDB & Mongoose for Performance
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents...
Read more >Make Mongoose Queries Faster without Losing Your Defaults
The folks at Mongoose have already thought about this and provided us with a neat solution, lean. Lean skips hydrating each document and...
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
There’s been some discussion about this before, can’t quite remember the issue. The point is that
ObjectId
is not a mongoose construct, but a fundamental mongodb construct. The point oflean()
is to bypass all of mongoose’s heavy change tracking and get the same result that you would get from running the query using the mongodb driver directly. Converting ObjectIds to their hex string representation would break that, so I guess this would require documentation changes more than anything else.What’s your use case for wanting
_id
as a string?JSON.stringify()
should convert it to a string for you just fine.Yeah. In my code I always just use
toHexString()
, this checks the ids for equality and ensures that both are probably mongodb objectids unless you happen to get another object with such a function.