[JAVA-258] Need oid in WriteResult Created: 13/Jan/11  Updated: 17/Mar/11  Resolved: 25/Feb/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.4
Fix Version/s: 2.5

Type: Improvement Priority: Major - P3
Reporter: Greg Zoller Assignee: Scott Hernandez (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Today if I insert or upsert a record and I want the oid for what was just inserted I have to look at the modified object.
But... I wrap these calls and usually return the WriteResult in case of an error. So its not easy to get to the oid in that case.
If the oid were included in WriteResult, with some kind of accessor, this would solve my problem.

In other words if I have a Storage object that has a save() method. I pass in serialized json to save().

public class Storage {
public WriteResult save( String jsonObj )

{ DBObject dbo = com.mongodb.util.JSON.parse(jsonObj); return mydbcollection.insert(...) }

}

The DBObject is created in save() so the caller never has access to it. My save() returns a WriteResult to check for errors. The modified _id that would be updated in my DBObject is lost when I return from save(). I could return some kind of tuple of (WriteResult, _id), but that's code smell. IMHO, a better option is to have WriteResult provide access to the _id--and I know it has it because it prints out in WriteResult.toString().



 Comments   
Comment by auto [ 15/Feb/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-258: prevent getLastError from being called in toString()
https://github.com/mongodb/mongo-java-driver/commit/07458f9bb366231b12bf51f438d9f07fcb5decbe

Comment by Antoine Girbal [ 15/Feb/11 ]

As said this depends on version of server, and is usually not included in commandresult like:
INFO:

{ "n" : 0 , "connectionId" : 67 , "wtime" : 0 , "err" : null , "ok" : 1.0}

The id is filed by the driver on client side, you can obtain it easily from the DBObject you insert using get("_id") after the insert.
This also works well if you are inserting several objects in same operation.

Fixed 1 thing from ticket: the toString() of WriteResult was calling getLastError() which can possibly request getLastError from server.
Such behavior should not happen in toString().
This is changed to getCachedLastError().

Comment by Scott Hernandez (Inactive) [ 15/Jan/11 ]

You can get the _id value from the dbo object; that is how you get the _id value.

The WriteResults comes from the server and the _id will be in the result of the GetLastError response only when the server is called in certain case – not all cases. There are many WriteConcerns for which this will not happen; so much of the time it will not be returned from the server.

You are welcome to write your application code any way you like, but the driver shouldn't do this.

Comment by Greg Zoller [ 13/Jan/11 ]

I dunno--didn't seem that convoluted to me: isolate storage code in helper classes rather than spread it out all over the place.

Doesn't WriteResult already has access to the resultant oid? It shows up if I do an insert then print out the toString() of the WriteResult.

I googled around and it appears that the Perl API just returns the oid as the return value of insert/update (true?)
my $id = $collection->insert(

{'name' => 'Alice', age => 20}

);

The Java API improves on that with a richer result object but unfortunately loses the OID in the process.

Comment by Scott Hernandez (Inactive) [ 13/Jan/11 ]

That is a seriously convoluted example.

You want something like this, done at the server (http://jira.mongodb.org/browse/SERVER-1173), where the _id is in the GLE response for save/insert; and this depends on the server version btw.

What you want should not be in the java api; this is an application issue...

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