0
votes

I'm chasing a severe bug in our application and after some digging I broke it down to this example. Using the jackson collection, I'm not able to remove documents. Usually it works for a single document which I just inserted. In the following example I just try to purge a unittest-collection.

import java.net.UnknownHostException;

import net.vz.mongodb.jackson.DBCursor;
import net.vz.mongodb.jackson.JacksonDBCollection;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.test.model.Account;

public class MongoTest {

  public static void main(String[] args) {
    try {
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("orion-unittest");

        // get a single collection
        DBCollection collection = db.getCollection("account");
        JacksonDBCollection<Account, String> jacksonCollection = JacksonDBCollection.wrap(collection, Account.class, String.class);

        // doesn't work
        DBCursor<Account> jacksonCursor = jacksonCollection.find();
        while (jacksonCursor.hasNext()) {
            jacksonCollection.remove(jacksonCursor.next());
        }
        System.out.println(jacksonCollection.getCount());

        // works!
        com.mongodb.DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            collection.remove(cursor.next());
        }
        System.out.println(collection.getCount());

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
  }

}

Console output:
6
0

The strange part ist, when I monitor the mongo server activities I see the delete commands being passed. do I miss something, or is this a bug of the jackson mapper?

here the Account class - nothing much there. the id is filled with the _id key:

import net.vz.mongodb.jackson.Id;

public class Account {
    @Id
    String id;
    String nickname = null;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}
2

2 Answers

0
votes

I haven't used mongo-jackson-mapper but as I saw in the source code it only calls dbCollection.remove method itself. So I guess your problem is with WriteConcern. try this one :

jacksonCollection.remove(jacksonCursor.next(), WriteConcern.FSYNC_SAFE);

and test 2 deletes separately (not one after another), so you can really understand if its deleting or not (it can have delete lag as well).

0
votes

I found the solution by using the @ObjectId instead of @Id. the problem was, that before my id was serialized to

{"_id": ObjectId("4f7551a74206b4f8e692bda3")} 

and not to something like:

{"_id": "4f7551a74206b4f8e692bda3"} 

... which failed the query done by remove trying to find the matching object. the @ObjectId annotation now equals the results of the serialization.