[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. In other words if I have a Storage object that has a save() method. I pass in serialized json to save(). public class Storage { } 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: |
| Comment by Antoine Girbal [ 15/Feb/11 ] |
|
As said this depends on version of server, and is usually not included in commandresult like: 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. Fixed 1 thing from ticket: the toString() of WriteResult was calling getLastError() which can possibly request getLastError from server. |
| 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?) ); 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... |