use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestDatastore method testUpdateWithCollation.
@Test
public void testUpdateWithCollation() {
getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
final Update<FacebookUser> update = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).update(inc("loginCount"));
UpdateResult results = update.execute();
assertEquals(results.getModifiedCount(), 1);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 0);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
results = update.execute(new UpdateOptions().multi(true).collation(Collation.builder().locale("en").collationStrength(SECONDARY).build()));
assertEquals(results.getModifiedCount(), 2);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 2);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestMaxMin method testMin.
@Test
public void testMin() {
final IndexedEntity a = new IndexedEntity("a");
final IndexedEntity b = new IndexedEntity("b");
final IndexedEntity c = new IndexedEntity("c");
Datastore ds = getDs();
ds.save(a);
ds.save(b);
ds.save(c);
Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("id")).hint("testField").min(new Document("testField", "b"))).next().id, b.id, "last");
Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("id")).hint("testField").min(new Document("testField", "b"))).next().id, b.id, "last");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestMaxMin method testMinCompoundIndex.
@Test
public void testMinCompoundIndex() {
final IndexedEntity a1 = new IndexedEntity("a");
final IndexedEntity a2 = new IndexedEntity("a");
final IndexedEntity b1 = new IndexedEntity("b");
final IndexedEntity b2 = new IndexedEntity("b");
final IndexedEntity c1 = new IndexedEntity("c");
final IndexedEntity c2 = new IndexedEntity("c");
Datastore ds = getDs();
ds.save(a1);
ds.save(a2);
ds.save(b1);
ds.save(b2);
ds.save(c1);
ds.save(c2);
List<IndexedEntity> l = ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).min(new Document("testField", "b").append("_id", b1.id))).toList();
Assert.assertEquals(l.size(), 4, "size");
Assert.assertEquals(l.get(0).id, b1.id, "item");
l = ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).min(new Document("testField", "b").append("_id", b1.id))).toList();
Assert.assertEquals(l.size(), 4, "item");
Assert.assertEquals(l.get(0).id, b1.id, "item");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestMaxMin method testMax.
@Test
public void testMax() {
final IndexedEntity a = new IndexedEntity("a");
final IndexedEntity b = new IndexedEntity("b");
final IndexedEntity c = new IndexedEntity("c");
Datastore ds = getDs();
ds.save(a);
ds.save(b);
ds.save(c);
ds.ensureIndexes();
Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(descending("id")).hint("testField").max(new Document("testField", "c"))).next().id, b.id, "last");
Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(descending("id")).hint("testField").max(new Document("testField", "c"))).next().id, b.id, "last");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testRegexInsensitiveQuery.
@Test
public void testRegexInsensitiveQuery() {
getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona")));
final Pattern p = Pattern.compile("(?i)caLifornia");
assertNotNull(getDs().find(PhotoWithKeywords.class).disableValidation().filter("keywords.keyword", p).execute(new FindOptions().limit(1)).next());
assertNull(getDs().find(PhotoWithKeywords.class).filter("keywords.keyword", Pattern.compile("blah")).execute(new FindOptions().limit(1)).tryNext());
}
Aggregations