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.

document.toJSON doesn't toJSON _id

See original GitHub issue

if you toJSON a mongoose model, the _id remains an object.

this is a problem if you’re trying to compare against an actual json representation of the same object.

Simple solution is to

JSON.parse(JSON.stringify(model))

but that’s real ugly.

Is there a good reason the id isn’t stringified? If not, I’ll fix up the toJSON in a PR.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:4
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
booxoodcommented, Sep 20, 2016

toJSON a mongoose model, the _id remains an object It is easy to make mistakes when make test compare…

0reactions
ZacharyRSmithcommented, Oct 6, 2017

In case this helpful to anyone, I had to create this perf-thinking lib to cast ObjectIDs to string.

var transformProps = require('transform-props');
 
function castToString(arg) {
    return String(arg);
}
 
var doc = new MongooseModel({ subDoc: { foo: 'bar' }});
var docObj = doc.toObject();
 
transformProps(docObj, castToString, '_id');
 
console.log(typeof docObj._id); // 'string'
console.log(typeof docObj.subDoc._id); // 'string'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bson Document to Json in Java - Stack Overflow
I use String id = ObjectId.get().toHexString(); rather than storing ObjectId directly. – Shadow Man. Apr 19, 2018 at 0:45.
Read more >
JSON And BSON - MongoDB
Firstly, BSON documents may contain Date or Binary objects that are not natively representable in pure JSON. Second, each programming language has its...
Read more >
C# calling tojson returns JSON that has"_id" - Google Groups
To work around this, I have been using Fields. Exclude("_id") but I'd like to retain this field in the json output. I have...
Read more >
Converting between JSON and XML - Json.NET
Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter. Elements, attributes, text, comments, character data, ...
Read more >
Working with JSON data in Google Standard SQL | BigQuery
This lets you store and query data that doesn't always adhere to fixed ... Row-level access policies cannot be applied on JSON columns....
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