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);
}
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());
}
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));
}
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);
}
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))));
}
Aggregations