[SERVER-1686] update doesn't add "_id" property when upserting Created: 25/Aug/10  Updated: 25/Aug/10  Resolved: 25/Aug/10

Status: Closed
Project: Core Server
Component/s: Usability
Affects Version/s: 1.6.1
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Mark Waschkowski Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I think that mongo should populate the "_id" field regardless if a traditional insert is done or a upsert is done.

Discussed here: http://groups.google.com/group/mongodb-user/browse_thread/thread/7f986a4609811c1d#

Created test case below:

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.xx.rm.GuiceModuleRiskManagement;

public class Test {

private static DB db = null;

private static String COLLECTION_NAME = "scoring";

public static void main(String[] args)

{ setup(); testSaveAndRetrieve(); }

public static void setup()

{ Injector injector = Guice.createInjector(new GuiceModuleRiskManagement()); db = injector.getInstance(DB.class); DBCollection col = db.getCollection(COLLECTION_NAME); col.drop(); }

public static void testSaveAndRetrieve()

{ String key = "actionItem"; BasicDBObject dbObject = new BasicDBObject(); dbObject.put("id", key); dbObject.put("test1", "test1 value"); dbObject.put("test2", "test2 value"); dbObject = save(key, dbObject, COLLECTION_NAME); assert(dbObject.get("_id") != null); }

public static BasicDBObject save(final String key, final BasicDBObject dbObject, final String collectionName){
final BasicDBObject finder = new BasicDBObject();
finder.put("id", key);

Command updateCommand = new Command() {
@Override
public Object execute()

{ db.getCollection(collectionName).update(finder, dbObject, true, false); System.out.println(dbObject.get("_id")); return dbObject; }

};
runDatabaseCommand(db, updateCommand);
return dbObject;
}

/**

  • Util method to follow proper mongodb idiom to allow all running threads to see changes made.
  • @param db
  • @param command
  • @return
    */
    public static Object runDatabaseCommand(DB db, Command command) {
    Object object = null;
    try
    Unknown macro: { db.requestStart(); object = command.execute(); DBObject dbObject = db.getLastError(); if(dbObject.get("err") != null){ throw new RuntimeException("Error:" + dbObject.get("err")); } db.requestDone(); }

    catch(Exception e)

    { e.printStackTrace(); throw new RuntimeException(e); }

    return object;
    }

public interface Command

{ public Object execute(); }

}



 Comments   
Comment by Eliot Horowitz (Inactive) [ 25/Aug/10 ]

See final comment in group discussion.

the server does add the _id, but its done server side not client side so you can't access it they way you are doing

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