@Test public void testFindAndRemove() throws Exception { Message m1 = new Message("Hello Spring"); template.insert(m1); Message m2 = new Message("Hello Mongo"); template.insert(m2); Query q = new Query(Criteria.where("text").regex("^Hello.*")); Message found1 = template.findAndRemove(q, Message.class); Message found2 = template.findAndRemove(q, Message.class); // Message notFound = template.findAndRemove(q, Message.class); DBObject notFound = template.getCollection("").findAndRemove(q.getQueryObject()); assertThat(found1, notNullValue()); assertThat(found2, notNullValue()); assertThat(notFound, nullValue()); }
@Test public void testEnsureIndex() throws Exception { Person p1 = new Person("Oliver"); p1.setAge(25); template.insert(p1); Person p2 = new Person("Sven"); p2.setAge(40); template.insert(p2); template .indexOps(Person.class) .ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); DBCollection coll = template.getCollection(template.getCollectionName(Person.class)); List<DBObject> indexInfo = coll.getIndexInfo(); assertThat(indexInfo.size(), is(2)); String indexKey = null; boolean unique = false; boolean dropDupes = false; for (DBObject ix : indexInfo) { if ("age_-1".equals(ix.get("name"))) { indexKey = ix.get("key").toString(); unique = (Boolean) ix.get("unique"); dropDupes = (Boolean) ix.get("dropDups"); } } assertThat(indexKey, is("{ \"age\" : -1}")); assertThat(unique, is(true)); assertThat(dropDupes, is(true)); List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo(); System.out.println(indexInfoList); assertThat(indexInfoList.size(), is(2)); IndexInfo ii = indexInfoList.get(1); assertThat(ii.isUnique(), is(true)); assertThat(ii.isDropDuplicates(), is(true)); assertThat(ii.isSparse(), is(false)); assertThat(ii.getFieldSpec().containsKey("age"), is(true)); assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true)); }