question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query 'lean' option doesn't produce POJOs

See original GitHub issue

The 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 _ids, 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Aug 29, 2016

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 of lean() 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.

0reactions
vkarpov15commented, Sep 17, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found