[JAVA-2477] Invalid ObjectId generated by MongoDB Created: 24/Mar/17 Updated: 27/Oct/23 Resolved: 22/Jun/17 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | BSON |
| Affects Version/s: | 3.4.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | KimiLiu [X] | Assignee: | Ross Lawley |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Cent OS 7 |
||
| Attachments: |
|
| Description |
|
Add some records to collection with java driver, it will generate object id for each records automatically. In my database, it generate below Object Id by MongoDB: It's invalid object id, my test: import org.bson.types.ObjectId; } Print result: The processIdentifier is a negative number. Now I map the object id to JSP file, then use JS to generate object id, the new object will contains endash, and it's an invalid object id. It happened sometimes but not frequently. The attachment is another invalid object id, notice the processIdentifier value. I'm not sure it's a java driver bug or core server bug. |
| Comments |
| Comment by Jeffrey Yemin [ 22/Jun/17 ] | |||||
|
There are several situations where the JVM will be unable to determine the process identifier. The driver makes a best effort to get one and then has several fallbacks (see org.bson.types.ObjectId#createProcessIdentifier for details), which as Ross said can result in a negative number in some situations. My advice is not to rely on the process identifier being a positive number. I suggest than you map an ObjectId to its hex string value in your JSP, rather than as a map of the constituent parts. | |||||
| Comment by KimiLiu [X] [ 27/Mar/17 ] | |||||
|
Hi, Lawley, Your answer makes me a litter confuse. import org.bson.types.ObjectId; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub String idStr = "58b38f407decdd8070b2df96"; ObjectId id = new ObjectId(idStr); System.out.println("process id is: " + id.getProcessIdentifier()); System.out.println("machine id is: " + id.getMachineIdentifier()); System.out.println("timestamp id is: " + id.getTimestamp()); }} 4. Check eclipse console, no Java error with below print result: Now I can get the process id is "-32656". In fact, my focus is whether a negative process identifier is right in objectid structure. I have ask this question in stack overflow but no response. As you said above: This can happen when the Java driver fails to get process identifier from JMX. But no java error report in my project, and most of objectid is right for me. | |||||
| Comment by Ross Lawley [ 24/Mar/17 ] | |||||
|
Hi Kimi, Thanks for the ticket, ObjectId("58b38f407decdd8070b2df96") is valid and perfectly usable as an ObjectId. The components of an ObjectId can be found in the documentation:
Which is why the ObjectId will happily roundtrip to and from the server:
How can I get a negative process id? This can happen when the Java driver fails to get process identifier from JMX. If this happens it falls back to using a random number instead and will log a warning message informing the user. I hope that answers your questions. For future reference, the best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support. All the best, Ross | |||||
| Comment by KimiLiu [X] [ 24/Mar/17 ] | |||||
|
bson jar version: 3.2.1 |