[SERVER-6986] ObjectId#toString and ObjectId#valueOf Created: 10/Sep/12  Updated: 15/Feb/13  Resolved: 28/Sep/12

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

Type: Question Priority: Minor - P4
Reporter: Alex Tsibulya Assignee: Eric Milkie
Resolution: Done Votes: 1
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-563 documentation for valueOf() and toStr... Closed
related to DOCS-586 Document "str" property of ObjectId()... Closed
related to SERVER-2684 special types in shell (e.g. Timestam... Closed
Participants:

 Description   

2.2.0 changed the behavior of ObjectId#toString and ObjectId#valueOf. In 2.0.x
ObjectId#toString would return an object ID string, like "50490f24a5b26b22e7000001", while in 2.2.0 it returns a string like "ObjectId(\"50490f24a5b26b22e7000001\")".

This change breaks our MR jobs that use object IDs as object literal keys, e.g.

var userEvent = {};
userEvent[this.user_id] = 1;
emit(key, userEvent);

Through some experimentation we found that ObjectId#valueOf in 2.2.0 returns a representation that we need, i.e. "50490f24a5b26b22e7000001". However, we also found that the behavior of ObjectId#valueOf was different in 2.0.x, where it return the object itself.

We couldn't find any documentation on ObjectId#valueOf and we're concerned that its behavior could change in future releases again. Can we rely on #valueOf remaining the same in the future? Or is there a more reliable way to convert ObjectId to a string, given that the 2.2.0 ObjectId#toString result is unusable for our purposes?



 Comments   
Comment by Eric Milkie [ 28/Sep/12 ]

I filed a documentation ticket and we're now actively working on documenting the overrides of .valueOf() and .toString() for the custom MongoDB Javascript types. I don't expect the behavior to change ever again; the change in 2.1 was done with the intent of standardizing the behavior – see SERVER-2684. The correct way, going forward, to extract the hexadecimal value of an ObjectId as a string is to use .valueOf().

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