Search in sources :

Example 71 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestIgnoreMissingReferences method testMissingReference.

public void testMissingReference() {
    final Container c = new Container();
    c.refs = new StringHolder[] { new StringHolder(), new StringHolder() };
    getDs().save(c);
    getDs().save(c.refs[0]);
    Container reloadedContainer = getDs().find(Container.class).iterator(new FindOptions().limit(1)).tryNext();
    assertNotNull(reloadedContainer);
    assertNotNull(reloadedContainer.refs);
    assertEquals(reloadedContainer.refs.length, 1);
    reloadedContainer = getDs().find(Container.class).filter(eq("_id", c.id)).first();
    assertNotNull(reloadedContainer);
    assertNotNull(reloadedContainer.refs);
    assertEquals(reloadedContainer.refs.length, 1);
    final List<Container> cs = getDs().find(Container.class).iterator().toList();
    assertNotNull(cs);
    assertEquals(cs.size(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions)

Example 72 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestReferences method testFindByEntityReference.

@Test
public void testFindByEntityReference() {
    final Ref ref = new Ref("refId");
    getDs().save(ref);
    final Container container = new Container();
    container.singleRef = ref;
    getDs().save(container);
    assertNotNull(getDs().find(Container.class).filter(eq("singleRef", ref)).iterator(new FindOptions().limit(1)).next());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DBRef(com.mongodb.DBRef) Test(org.testng.annotations.Test)

Example 73 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class AggregationTest method testLookup.

/**
 * Test data pulled from https://docs.mongodb.com/v3.2/reference/operator/aggregation/lookup/
 */
@Test
public void testLookup() {
    getDs().save(asList(new Order(1, "abc", 12, 2), new Order(2, "jkl", 20, 1), new Order(3)));
    List<Inventory> inventories = asList(new Inventory(1, "abc", "product 1", 120), new Inventory(2, "def", "product 2", 80), new Inventory(3, "ijk", "product 3", 60), new Inventory(4, "jkl", "product 4", 70), new Inventory(5, null, "Incomplete"), new Inventory(6));
    getDs().save(inventories);
    getDs().aggregate(Order.class).lookup(lookup("inventory").localField("item").foreignField("sku").as("inventoryDocs")).out(to(Lookedup.class));
    List<Order> lookups = getDs().find("lookups", Order.class).iterator(new FindOptions().sort(ascending("_id"))).toList();
    Assert.assertEquals(inventories.get(0), lookups.get(0).inventoryDocs.get(0));
    Assert.assertEquals(inventories.get(3), lookups.get(1).inventoryDocs.get(0));
    Assert.assertEquals(inventories.get(4), lookups.get(2).inventoryDocs.get(0));
    Assert.assertEquals(inventories.get(5), lookups.get(2).inventoryDocs.get(1));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 74 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations method testUpdateWithDifferentType.

@Test
public void testUpdateWithDifferentType() {
    final ContainsInt cInt = new ContainsInt();
    cInt.val = 21;
    getDs().save(cInt);
    Query<ContainsInt> query = getDs().find(ContainsInt.class);
    final UpdateResult res = query.update(inc("val", 1.1D)).execute();
    assertUpdated(res, 1);
    Assert.assertEquals(query.iterator(new FindOptions().limit(1)).next().val, 22);
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 75 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations method testRemoveAllList.

@Test
public void testRemoveAllList() {
    LogHolder logs = new LogHolder();
    Date date = new Date();
    logs.logs.addAll(asList(new Log(1), new Log(2), new Log(3), new Log(1), new Log(2), new Log(3)));
    Datastore ds = getDs();
    ds.save(logs);
    UpdateResult results = ds.find(LogHolder.class).update(pullAll("logs", singletonList(new Log(3)))).execute();
    Assert.assertEquals(results.getModifiedCount(), 1);
    LogHolder updated = ds.find(LogHolder.class).iterator(new FindOptions().limit(1)).next();
    Assert.assertEquals(updated.logs.size(), 4);
    Assert.assertTrue(updated.logs.stream().allMatch(log -> log.equals(new Log(1)) || log.equals(new Log(2))));
}
Also used : Document(org.bson.Document) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) CoreMatchers(org.hamcrest.CoreMatchers) Filters.regex(dev.morphia.query.experimental.filters.Filters.regex) Date(java.util.Date) Embedded(dev.morphia.annotations.Embedded) Test(org.testng.annotations.Test) Book(dev.morphia.test.models.Book) Datastore(dev.morphia.Datastore) Id(dev.morphia.annotations.Id) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) UpdateOperators.pullAll(dev.morphia.query.experimental.updates.UpdateOperators.pullAll) TypeSpecification(dev.morphia.query.experimental.updates.CurrentDateOperator.TypeSpecification) UpdateOperators.pull(dev.morphia.query.experimental.updates.UpdateOperators.pull) UpdateOperators.setOnInsert(dev.morphia.query.experimental.updates.UpdateOperators.setOnInsert) UpdateResult(com.mongodb.client.result.UpdateResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) UpdateOperators.dec(dev.morphia.query.experimental.updates.UpdateOperators.dec) Entity(dev.morphia.annotations.Entity) Circle(dev.morphia.test.models.Circle) UpdateOperators.addToSet(dev.morphia.query.experimental.updates.UpdateOperators.addToSet) UpdateOptions(dev.morphia.UpdateOptions) ValidationException(dev.morphia.query.ValidationException) User(dev.morphia.test.models.User) PreLoad(dev.morphia.annotations.PreLoad) FindOptions(dev.morphia.query.FindOptions) UpdateOperators.pop(dev.morphia.query.experimental.updates.UpdateOperators.pop) Set(java.util.Set) UpdateOperators.min(dev.morphia.query.experimental.updates.UpdateOperators.min) Assert.assertNotNull(org.testng.Assert.assertNotNull) Instant(java.time.Instant) Objects(java.util.Objects) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) UpdateOperators.xor(dev.morphia.query.experimental.updates.UpdateOperators.xor) MorphiaCursor(dev.morphia.query.MorphiaCursor) Query(dev.morphia.query.Query) UpdateOperators.and(dev.morphia.query.experimental.updates.UpdateOperators.and) LocalDate(java.time.LocalDate) Expressions.literal(dev.morphia.aggregation.experimental.expressions.Expressions.literal) UpdateOperators.set(dev.morphia.query.experimental.updates.UpdateOperators.set) UpdateOperators.max(dev.morphia.query.experimental.updates.UpdateOperators.max) UpdateOperators.inc(dev.morphia.query.experimental.updates.UpdateOperators.inc) Set.set(dev.morphia.aggregation.experimental.stages.Set.set) Assert.assertNull(org.testng.Assert.assertNull) UpdateOperators.push(dev.morphia.query.experimental.updates.UpdateOperators.push) LocalDateTime(java.time.LocalDateTime) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) DeleteOptions(dev.morphia.DeleteOptions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Update(dev.morphia.query.Update) Calendar(java.util.Calendar) Assert(org.testng.Assert) Assert.assertThrows(org.testng.Assert.assertThrows) BsonTimestamp(org.bson.BsonTimestamp) Sort(dev.morphia.query.Sort) Rectangle(dev.morphia.test.models.Rectangle) UpdateOperators.rename(dev.morphia.query.experimental.updates.UpdateOperators.rename) UpdateOperators.mul(dev.morphia.query.experimental.updates.UpdateOperators.mul) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UpdateOperators.unset(dev.morphia.query.experimental.updates.UpdateOperators.unset) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) TestEntity(dev.morphia.test.models.TestEntity) Month(java.time.Month) PathTarget(dev.morphia.internal.PathTarget) Matchers(org.hamcrest.Matchers) Filters(dev.morphia.query.experimental.filters.Filters) Indexed(dev.morphia.annotations.Indexed) UpdateOperators.or(dev.morphia.query.experimental.updates.UpdateOperators.or) DAYS(java.time.temporal.ChronoUnit.DAYS) Matchers.hasItem(org.hamcrest.Matchers.hasItem) NOW(dev.morphia.aggregation.experimental.expressions.SystemVariables.NOW) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) StringJoiner(java.util.StringJoiner) Filters.eq(dev.morphia.query.experimental.filters.Filters.eq) Pic(dev.morphia.test.query.TestQuery.Pic) ObjectId(org.bson.types.ObjectId) Nullable(com.mongodb.lang.Nullable) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) UpdateResult(com.mongodb.client.result.UpdateResult) 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