[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: PNG File Untitled.png    

 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:
ObjectId("58b38f407decdd8070b2df96")

It's invalid object id, my test:

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(id.getProcessIdentifier()); }

}

Print result:
-32656

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.
Firstly, you can add the objectid i given in monodb command, that right.
Secondly, How can I get a negative process id? Because my project is Java project, so I make a test in eclipse withe below steps:
1. open new Java project, and create a "test" class
2. import bson-3.2.1 jar file to project.
3. coding in test class as below:

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:
process id is: -32656
machine id is: 8252637
timestamp id is: 1488162624

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:

The 12-byte ObjectId value consists of:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.

Which is why the ObjectId will happily roundtrip to and from the server:

MongoDB Enterprise > db.test.insert({_id: ObjectId("58b38f407decdd8070b2df96") })
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.test.findOne()
{ "_id" : ObjectId("58b38f407decdd8070b2df96") }
MongoDB Enterprise >

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
JDK: 1.8.111
mongo db version: 3.4.1

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