I create a new document, just like the Java Tutorial says, by creating a new BasicDBObject, then calling put several times. I store the object by calling collection.insert(doc). (I also tried collection.save(...), with the same result.)
Now, when calling doc.get("_id"), I get a different ID than the one which I can see in my DB by using the mongo shell. It does not matter if I call the get method on my initial doc object, or on the return value of the insert call.
The only workaround I can currently think of would be to assign an arbitrary ID to the document, for example doc.put("my_id", arbitraryID), then after storing the document, retrieving it by finding the one with the arbitrary ID, and take it's "_id". But that would somehow defeat the purpose of "_id" and be more of a bad hack.
I am using the latest mongodb on OS X, and the 0.6 Java driver.
I create a new document, just like the Java Tutorial says, by creating a new BasicDBObject, then calling put several times. I store the object by calling collection.insert(doc). (I also tried collection.save(...), with the same result.)
Now, when calling doc.get("_id"), I get a different ID than the one which I can see in my DB by using the mongo shell. It does not matter if I call the get method on my initial doc object, or on the return value of the insert call.
The only workaround I can currently think of would be to assign an arbitrary ID to the document, for example doc.put("my_id", arbitraryID), then after storing the document, retrieving it by finding the one with the arbitrary ID, and take it's "_id". But that would somehow defeat the purpose of "_id" and be more of a bad hack.
I am using the latest mongodb on OS X, and the 0.6 Java driver.