@Test @SuppressWarnings("deprecation") public void testReturnOnlyIndexedFields() { getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4"))); getDs().ensureIndex(Pic.class, "name"); // When // find a document by using a search on the field in the index // Then Pic foundItem = getDs().find(Pic.class) .returnKey() .field("name").equal("pic2") .find(new FindOptions().limit(1) .modifier("$returnKey", true)) .tryNext(); assertNotNull(foundItem); assertThat("Name should be populated", foundItem.getName(), is("pic2")); assertNull("ID should not be populated", foundItem.getId()); }
@Test @SuppressWarnings("deprecation") public void testMultipleIndexedFields() { final MappedClass mc = getMorphia().getMapper().getMappedClass(Ad.class); getMorphia().map(Ad.class); assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), doesNotHaveIndexNamed("lastMod_1_active_-1")); getDs().ensureIndex(Ad.class, "lastMod, -active"); assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("lastMod_1_active_-1")); }