1

While attempt to work with MongoJack it suddenly started to return null with every findOneById query. Even though the object is being created in the database and the ID returned is valid.

Code

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

public class test {

    public static void main(String[] args){
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB( "test" );

        DBCollection dbCollection = db.getCollection("myobject");
        JacksonDBCollection<MyObject, String> coll = JacksonDBCollection.wrap(dbCollection, MyObject.class,
            String.class);
        MyObject myObject = new MyObject();
        WriteResult<MyObject, String> result = coll.insert(myObject);
        String id = result.getSavedId();
        MyObject savedObject = coll.findOneById(id);

        System.out.println(savedObject);

    }

}

class MyObject{
    @Id
    private String id;
}

The output of which is:

null
Process finished with exit code 0

Yet a record is being created with the ID in the database.

  • I was having the same issue, although I never had it working in the first place. Did you start seeing this behavior after a MongoJack update? – Treur Oct 20 '16 at 13:12

1 Answers1

2

I found that simply adding the @ObjectId annotation to the id of the class MyObject seems to fix the problem

class MyObject{
    @Id @ObjectId
    private String id;
}