Search in sources :

Example 86 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 87 with FindOptions

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");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 88 with FindOptions

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");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 89 with FindOptions

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");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 90 with FindOptions

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());
}
Also used : Pattern(java.util.regex.Pattern) FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Aggregations

FindOptions (dev.morphia.query.FindOptions)118 Test (org.testng.annotations.Test)114 Point (com.mongodb.client.model.geojson.Point)16 Position (com.mongodb.client.model.geojson.Position)16 Document (org.bson.Document)16 Rectangle (dev.morphia.test.models.Rectangle)13 Datastore (dev.morphia.Datastore)12 UpdateResult (com.mongodb.client.result.UpdateResult)8 UpdateOptions (dev.morphia.UpdateOptions)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (dev.morphia.query.ValidationException)6 User (dev.morphia.test.models.User)5 ContainsPic (dev.morphia.test.query.TestQuery.ContainsPic)5 Pic (dev.morphia.test.query.TestQuery.Pic)5 LocalDate (java.time.LocalDate)5 Date (java.util.Date)5 DeleteOptions (dev.morphia.DeleteOptions)4 Query (dev.morphia.query.Query)4 DocumentValidation (dev.morphia.test.models.DocumentValidation)4 ContainsPic (dev.morphia.test.query.TestLegacyQuery.ContainsPic)4