Constructing queries is really just creating BSON document representation, which is basically just the same interface as standard HashMap or List interfaces as appropriate:
Document query = new Document("objectKey",new Document( "$regex","Bos"))
.append("cacheVersionString","08/03/15_11:05:09");
Document projection = new Document("_id",0)
.append("objectData",0)
.append("lastModified",0)
.append("productCode",0);
MongoCursor<Document> cursor = collection.find(query).projection(projection).iterator();
Where that is basically identical to how you are structuring queries in the MongoDB shell.
Alternately you can use builder interfaces if that seems more logical to you:
QueryBuilder builder = QueryBuilder.start();
builder.and("objectKey").regex(Pattern.compile("box"));
builder.and("cache_version_string").is("08/03/15_11:05:09");
BasicDBObject query = (BasicDBObject)builder.get();
Bson projection = Projections.exclude(
"_id",
"obectdata",
"lasModified",
"productCode"
);
MongoCursor<Document> cursor = collection.find(query).projection(projection).iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.println(doc.toJson());
}
Both forms essentially contruct the BSON for both the "query" and "projection" components and issue them as arguments to the .find() method. There are also class type definitions if that suits you as well.