[SERVER-2426] Convert/print bson undefined type as javascript undefined, not null Created: 27/Jan/11  Updated: 11/Nov/15  Resolved: 11/Nov/15

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: David Hu Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-14221 Distinct doesn't properly handle unde... Closed
Related
related to SERVER-6102 Shell displays both 'undefined' and '... Closed
is related to SERVER-19503 Javascript object serialization proce... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

This snippet shows how an "Undefined" (0x06) BSON value may get stored into the database.

use tempDB;
var obj = {};
db.tempColl.insert(

{ data: obj.undefinedField }

);
db.tempColl.find(

{ data: null }

); // expects one document but actually returns zero documents.
db.tempColl.find({ data: { $type: 10 }}); // expects one document but actually returns zero documents.
db.tempColl.find({ data: { $type: 6 }}); // expects zero documents but returns one document.



 Comments   
Comment by Jonathan Reams [ 11/Nov/15 ]

Duplicate of SERVER-6102

Comment by Eliot Horowitz (Inactive) [ 31/Jan/11 ]

Agree it has to be consistent, but there are some reasons to keep undefined.

So the real task is clearly laying out all js conversions and making sure its consistent.

Comment by David Hu [ 28/Jan/11 ]

If you retrieve a document containing "undefined" BSON values and save it back into the collection, those "undefined" BSON will values get converted to "null" BSON values. Here's an example:

> db.temp.save({_id:1, b:{}.undefinedField});
> var doc = db.temp.findOne({_id:1, b:{$type: 6}});
> doc

{ "_id" : 1, "b" : null }

> doc.c = "str";
str
> db.temp.save(doc);
> db.temp.findOne({_id:1, b:{$type:6}});
null
> db.temp.findOne({_id:1, b:{$type:10}});

{ "_id" : 1, "b" : null, "c" : "str" }

In v8_wrapper's mongoToV8Element function, "undefined" elements (0x06) get converted to v8::Null(). However, the v8ToMongoElement function converts v8 "undefined" elements to BSON "undefined" elements.

Comment by Scott Hernandez (Inactive) [ 28/Jan/11 ]

It is even easier than that.

> printjson(

{a:undefined}

)

undefined is a valid value in javascript, and in bson.

What is the motivation for removing it? And why set it to null? It seems like if a field value is undefined then the field should be skipped, not set to null.

Generated at Thu Feb 08 02:59:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.