Fork me on GitHub


MongoJack 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.


org.mongojack.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("username", "jsmith"));

Multiple operators can be chained together:

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


org.mongojack.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();


MongoJack provides a convenient helper utility for building sort specifications called org.mongojack.DBSort:

List<BlogPost> posts = coll.find().sort(DBSort.desc("date")).toArray();


If you don’t want to load the entire object, you can use projections to either include or exclude fields. MongoJack provides a helper utility for building these called org.mongojack.DBProjection:

List<BlogPost> posts = coll.find("published", true),
        DBProjection.include("title", "author")).toArray();


MongoJack will attempt to serialise values according to the serialisation configuration of the bean. This is, however, only done on a best effort basis. In some cases it may not be possible to serialise all values, for example, when using polymorphic types or ungenercised collections.