[SERVER-19503] Javascript object serialization process should skip properties with undefined values Created: 21/Jul/15 Updated: 03/Apr/23 Resolved: 11/Nov/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | JavaScript |
| Affects Version/s: | 3.0.3 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Víctor Herraiz Posada | Assignee: | Jonathan Reams |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||
| Sprint: | Build 8 08/31/15, Build 9 (09/18/15), Build A (10/09/15), Build B (10/30/15), Build C (11/20/15) | ||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
Inserting a "undefined" field (e.i. a: undefined) results in a documenta with a "null" value in that field. In my opinión those field should be ignored and not transformed, same as JSON.stringify. This could be reproduced in the mongodb console and in the node driver |
| Comments |
| Comment by Jonathan Reams [ 11/Nov/15 ] | ||||||||||
|
We've decided to deserialize undefined BSON values as undefined javascript values (see | ||||||||||
| Comment by Mathias Stearn [ 09/Sep/15 ] | ||||||||||
|
Linking to I'd vote for preserving undefined to ensure that documents round-trip correctly from bson->js->bson and js->bson->js. If you do not want the field in the document, you can still delete it rather than setting to undefined. | ||||||||||
| Comment by Víctor Herraiz Posada [ 21/Jul/15 ] | ||||||||||
|
Thank you Jason, your summary is better than mine | ||||||||||
| Comment by J Rassi [ 21/Jul/15 ] | ||||||||||
|
Thanks for clarifying. Updating component to "Javascript", updating summary to "Javascript object serialization process should skip properties with undefined values", and sending to the platform team for triage. | ||||||||||
| Comment by Víctor Herraiz Posada [ 21/Jul/15 ] | ||||||||||
|
Yes, that's is. JSON do not support undefined values, therefore if you try to serialise {a: undefined, b: 1} you end up with {b:1} usign JSON.stringify. In my opinion, MongoDB and drivers (Javascript), should behave the same way, and do not change the undefined values to null. | ||||||||||
| Comment by J Rassi [ 21/Jul/15 ] | ||||||||||
|
I'm not familiar with the node.js driver, but I can speak briefly about the shell's behavior. The shell actually serializes the Javascript object {a: undefined} into the BSON object {a: undefined}, but it deserializes the BSON object {a: undefined} into the Javascript object {a: null} (this peculiarity is documented in the codebase with a comment "duplicate [SpiderMonkey] behavior", but I'm not familiar enough with SpiderMonkey to know exactly what it refers to). So, it's technically not correct that inserting a document with an undefined value from the shell results in a document with a null value, it's just that the shell treats it as having a null value when it receives it back from the server. See the following example:
victorherraiz: if I understand you correctly, you are requesting here that db.foo.insert({a: undefined}) results in an insert of the empty document {}. Is this correct? |