Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1686

update doesn't add "_id" property when upserting

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.6.1
    • Component/s: Usability
    • Labels:
      None

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

      }

            Assignee:
            Unassigned Unassigned
            Reporter:
            mwaschkowski Mark Waschkowski
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: