[SERVER-7055] ObjectId to String type conversion different in 2.2 from 2.0 Created: 17/Sep/12  Updated: 15/Feb/13  Resolved: 07/Oct/12

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

Type: Bug Priority: Major - P3
Reporter: Joe Gornick Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-584 Release notes for 2.2 should mention ... Closed
related to DOCS-586 Document "str" property of ObjectId()... Closed
related to SERVER-2684 special types in shell (e.g. Timestam... Closed
is related to DOCS-563 documentation for valueOf() and toStr... Closed
Operating System: ALL
Participants:

 Description   

The type conversion from ObjectId to String is invalid compared to previous MongoDB versions.

In Mongo 2.0.x:

var oid = new ObjectId("50575c152c2414982d000003");
var oidAsString = String(oid);
print(typeof oidAsString); // string
print(oidAsString); // 50575c152c2414982d000003

In Mongo 2.2.x:

var oid = new ObjectId("50575c152c2414982d000003");
var oidAsString = String(oid);
print(typeof oidAsString); // string
print(oidAsString); // ObjectId("50575c152c2414982d000003")

I would expect the converted value to be the hex string as described in 2.0.x.

This issue is similar to SERVER-5439, however, this is not an improvement but a bug.



 Comments   
Comment by Tad Marshall [ 07/Oct/12 ]

Hi Joe,

Thanks for reporting this breaking change.

We did change the behavior of the toString(), valueOf() and (indirectly) tojson() methods in version 2.2. The change was part of the fix for SERVER-2684, but the change was not marked as a breaking change as it should have been. I have corrected that, and I have created DOCS-584 to get this information into the release notes.

The workaround you discovered will be our documented way of dealing with this incompatibility between the versions; thanks for suggesting it! I filed DOCS-586 to get this usage added to the documentation.

DOCS-584 contains more information on workarounds, including some code that you can run to restore the previous behavior of the ObjectId methods if you want to do that.

Sorry about the breakage; it was an attempt to make the API more uniform and it achieves that purpose, but it should have been flagged as a breaking change and workarounds should have been suggested. We don't expect to change it again, and we'll try to do a better job of announcing breaking changes like this in the release notes.

Tad

Comment by Joe Gornick [ 17/Sep/12 ]

A workaround I've found is to access the non-documented "str" property on an ObjectId.

var oid = new ObjectId("50575c152c2414982d000003");
print(typeof oid.str); // string
print(oid.str); // 50575c152c2414982d000003

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