Querying

The Mongo Jackson Mapper allows you to query MongoDB in exactly the same way that you query using the Java MongoDB driver, with a few extra features for convenience.

DBQuery

net.vz.mongodb.jackson.DBQuery is a utility class for building queries, which provides methods that implement the query operators that MongoDB provides. It is similar to com.mongodb.QueryBuilder, however it allows a slightly more terse syntax. The most simple element it supports is is, for example:

coll.find(DBQuery.is("username", "jsmith"));

Multiple operators can be chained together:

coll.find(DBQuery.greaterThan("age", 21).exists("parent"));

DBCursor

net.vz.mongodb.jackson.DBCursor also implements the same DBQuery interface, allowing even simpler chaining of commands:

List<BlogPost> posts = coll.find().in("tags", "mongodb", "java", "jackson")
        .is("published", true).limit(10).toArray();

Serialization

The Mongo Jackson Mapper will attempt to serialise any query values that it doesn’t recognise as simple types using Jackson, this includes enums and other beans. If you have custom serializers declared for those enums/beans, it will use those. However, if you supply a value for a field that has a custom serailiser declared at the field level, the Mongo Jackson Mapper is currently not able to honor that. For example, if you have a field declared to be an @ObjectId, you will need convert the value to an ObjectId before passing it into the query:

coll.find().is("category", new ObjectId(categoryId));