Search code examples
javamongodbmongojack

Use of MongoJack and updateById


I am having a problem with the usage of MongoJack and updateById method.

In the javadoc from MongoJack, it states that the method updateById(K, T) (link to javadoc) can be used with K as the key and T as the object to save. However, the following code and test shows that this is not the case. The full source code can be found at Github: https://github.com/nilsmagnus/mongojackupdatebyid/.

Object to save:

package no.nils.mongoupdate;

  public class Scenario {
  public String _id;
  public String name;

  public Scenario(String name) {
    this.name = name;
  }

  public String get_id() {
    return _id;
  }

  public String getName() {
    return name;
  }
}

Updating class:

package no.nils.mongoupdate;

import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;

class ScenarioStorage {
private JacksonDBCollection<Scenario, String> scenarioCollection;

public ScenarioStorage(DB db) {
    DBCollection dbCollection = db.getCollection("scenario");
    scenarioCollection = JacksonDBCollection.wrap(dbCollection, Scenario.class, String.class);
}

public void update(Scenario scenario) {
    WriteResult<Scenario, String> writeResult = scenarioCollection.updateById(scenario._id, scenario);
    if (writeResult.getError() != null) throw new RuntimeException(writeResult.getError());
}

public String saveNewScenario(Scenario scenario) {
    WriteResult<Scenario, String> writeResult = scenarioCollection.insert(scenario);
    if (writeResult.getError() != null) throw new RuntimeException(writeResult.getError());
    return writeResult.getSavedId();
}


public Scenario getScenario(String id) {
    return scenarioCollection.findOneById(id);
}
}

Test that shows the failure:

package no.nils.mongoupdate;

import java.net.UnknownHostException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;
import com.mongodb.DB;
import com.mongodb.MongoClient;

public class ScenarioStorageTest {

private ScenarioStorage storage;

@Before
public void createDb() throws UnknownHostException {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB db = mongoClient.getDB("junittest");
    storage = new ScenarioStorage(db);
}

@Test
public void shouldUpdateNameAfterUpdate() {
    Scenario scenario = new Scenario("first");
    final String id = storage.saveNewScenario(scenario);
    scenario._id = id;
    scenario.name = "newname";
    storage.update(scenario);
    Scenario updated = storage.getScenario(id);
    assertNotNull("Stored object should have been fetched.", updated.name);
    assertEquals("The stored object should now have the name 'newname'.", updated.name, "newname");
}
}

The test fails on the first assertion. Please advise if I have misunderstood the javadoc for mongojack or any other errors I may have made.


Solution

  • Ok, the javadoc and tutorial is broken. The solution is to add the annotation @ObjectId to the _id field.

    @ObjectId
    public String _id;