[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:
Duplicate
is duplicated by SERVER-6102 Shell displays both 'undefined' and '... Closed
Related
related to SERVER-2426 Convert/print bson undefined type as ... Closed
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 SERVER-6102).

Comment by Mathias Stearn [ 09/Sep/15 ]

Linking to SERVER-2426 which requests that BSON Undefined values become js undefined rather than null. Whatever we do with this ticket should probably be consistent for both the js->bson and bson->js conversions.

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:

> db.foo.drop()
true
> db.foo.insert({a:undefined})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({a:null})
WriteResult({ "nInserted" : 1 })
> db.foo.find({a:{$type:6}}) // 6 == undefined
{ "_id" : ObjectId("55ae64a5fc967df3c800adfe"), "a" : null } // This document has an undefined value, but shell is treating it as null.
> db.foo.find({a:{$type:10}}) // 10 == null
{ "_id" : ObjectId("55ae64b0fc967df3c800adff"), "a" : null } // This document has a null value.

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?

Generated at Thu Feb 08 03:51:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.