MongoQueryTranslator extends
        AbstractQueryTranslator<DBObject, DBObject, DBObject> implements
        QueryTranslator<DBObject, DBObject, DBObject> { @Override public <T, R> DBObject translateProjection(Query<T, R> query) { if (query.getReturnFields().size() > 0) { DBObject fields = object(); for(String field : query.getReturnFields()) { fields.put( getPropertyResolver().resolve(field, query.getEntityClass()), 1); } if (!fields.containsField(Mapper.ID_KEY)) { fields.put(Mapper.ID_KEY, 0); } return fields; } else { return null; } } MongoQueryTranslator(PropertyResolver propertyResolver); @Override DBObject eq(String fieldName, Object value); @Override DBObject ne(String fieldName, Object value); @Override DBObject lt(String fieldName, Object value); @Override DBObject lte(String fieldName, Object value); @Override DBObject gt(String fieldName, Object value); @Override DBObject gte(String fieldName, Object value); @Override DBObject between(String fieldName, Object from, Object to); @Override DBObject in(String fieldName, Object[] values); @Override DBObject notIn(String fieldName, Object[] values); @Override DBObject contains(String fieldName, Object[] values); @Override DBObject isNull(String fieldName); @Override DBObject notNull(String fieldName); @Override DBObject isEmpty(String fieldName); @Override DBObject notEmpty(String fieldName); @Override DBObject within(String fieldName,
            Point<N> center, Number distance); @Override DBObject and(DBObject... subqueries); @Override DBObject or(DBObject... subqueries); @Override DBObject order(String fieldName, Order o); @Override DBObject order(DBObject... orders); @Override DBObject translateProjection(Query<T, R> query);  }
@Test public void translateProjection_someWithId() { String[] properties = new String[] { "id", "name", "date", "thisProperty" }; String[] fields = new String[] { "_id", "name", "date", "thatProperty" }; DBObject expected = o(); for (String f : fields) { expected.put(f, 1); } assertEquals(expected, translator.translateProjection(QueryBuilder.builderFor( TestClass.class, TestClass.class, properties).build())); }
@Test public void translateProjection_someWithoutId() { String[] properties = new String[] { "name", "date", "thisProperty" }; String[] fields = new String[] { "name", "date", "thatProperty" }; DBObject expected = o(); for (String f : fields) { expected.put(f, 1); } expected.put("_id", 0); assertEquals(expected, translator.translateProjection(QueryBuilder.builderFor( TestClass.class, TestClass.class, properties).build())); }