Documentation: clarify the difference between toObject() and toJSON()
See original GitHub issueWhat is the difference between these two? It’s not immediately apparent which I should be targeting when I use a transform. Up until now I have just blindly added the same transform on both.
My use case: I want to be able to modify the json that is sent to the front end (through express), but I don’t want this modified data to be persisted to the database whenever a save()
is called.
Issue Analytics
- State:
- Created 9 years ago
- Comments:6
Top Results From Across the Web
What is the difference between Mongoose toObject and ...
toJSON is exactly the same as the toObject option but only applies when the document's toJSON method is called. Is toJSON an alias...
Read more >Mongoose toObject and toJSON transform behavior with sub ...
So it looks like toObject is being applied to sub-documents while toJSON is not. And this is different from what the documentation says:....
Read more >Mongoose v6.8.1: Document
«Boolean,Document» whether mongoose thinks this doc is deleted. ... There is one difference between toJSON() and toObject() options. When you call toJSON() ...
Read more >Working with JSON - Learn web development | MDN
Converting between objects and text · parse() : Accepts a JSON string as a parameter, and returns the corresponding JavaScript object. · stringify ......
Read more >json — JSON encoder and decoder — Python 3.11.1 ...
The other arguments have the same meaning as in load() . If the data being deserialized is not a valid JSON document, a...
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 FreeTop 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
Top GitHub Comments
To all y’all finding this in 2019…
JSON.stringify checks objects passed to it for a function called toJSON. If it exists it uses that function to determine the object that should be stringified. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior
If you are using express, when calling res.send(object) and the client accepts JSON it will JSON.stringify the object passed into send. This means when you pass a mongoose model directly into res.send(model) it will send the toJSON format. If you just need to get an object representation of your model then I would use toObject.
If you ever have a use case where you want your models turned into objects in two different ways, then it makes sense to have both. If you want to be able to easily res.send(model) you need toJSON, and if you ever want to toObject that produces a result other than toJSON you need that to run with it’s own options.
toJSON seems to essentially just exist for compatibility with utilities that already call toJSON such as JSON.stringify.
A difference between
toJSON
andtoObject
I’ve found is that properties on the schema defined as a map seem to be set to an empty object when callingtoObject
.I’m not sure if this behaviour is intended or not but makes toJSON much more useful in the case the record contains a map.