use of com.mongodb.BasicDBObject in project morphia by mongodb.
the class UpdateOpsImpl method push.
@Override
public UpdateOperations<T> push(final String field, final List<?> values, final PushOptions options) {
if (values == null || values.isEmpty()) {
throw new QueryException("Values cannot be null or empty.");
}
PathTarget pathTarget = new PathTarget(mapper, mapper.getMappedClass(clazz), field);
if (!validateNames) {
pathTarget.disableValidation();
}
BasicDBObject dbObject = new BasicDBObject(UpdateOperator.EACH.val(), mapper.toMongoObject(pathTarget.getTarget(), null, values));
options.update(dbObject);
addOperation(UpdateOperator.PUSH, pathTarget.translatedPath(), dbObject);
return this;
}
use of com.mongodb.BasicDBObject in project morphia by mongodb.
the class TestIndexCollections method testIndex.
private void testIndex(final List<DBObject> indexInfo, final BasicDBObject... indexes) {
Iterator<DBObject> iterator = indexInfo.iterator();
while (iterator.hasNext()) {
final DBObject dbObject = iterator.next();
if (dbObject.get("name").equals("_id_")) {
iterator.remove();
} else {
for (final DBObject index : indexes) {
final DBObject key = (DBObject) dbObject.get("key");
if (key.equals(index)) {
iterator.remove();
}
}
}
}
Assert.assertTrue("Should have found all the indexes. Remaining: " + indexInfo, indexInfo.isEmpty());
}
use of com.mongodb.BasicDBObject in project morphia by mongodb.
the class TestInterfaces method testDynamicInstantiation.
@Test
public void testDynamicInstantiation() throws Exception {
final DBCollection shapes = getDb().getCollection("shapes");
final DBCollection shapeshifters = getDb().getCollection("shapeshifters");
getMorphia().map(Circle.class).map(Rectangle.class).map(ShapeShifter.class);
final Shape rectangle = new Rectangle(2, 5);
final DBObject rectangleDbObj = getMorphia().toDBObject(rectangle);
shapes.save(rectangleDbObj);
final BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());
assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
assertTrue(rectangleLoaded instanceof Rectangle);
final ShapeShifter shifter = new ShapeShifter();
shifter.setReferencedShape(rectangleLoaded);
shifter.setMainShape(new Circle(2.2));
shifter.getAvailableShapes().add(new Rectangle(3, 3));
shifter.getAvailableShapes().add(new Circle(4.4));
final DBObject shifterDbObj = getMorphia().toDBObject(shifter);
shapeshifters.save(shifterDbObj);
final BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache());
assertNotNull(shifterLoaded);
assertNotNull(shifterLoaded.getReferencedShape());
assertNotNull(shifterLoaded.getReferencedShape().getArea());
assertNotNull(rectangle);
assertNotNull(rectangle.getArea());
assertTrue(rectangle.getArea() == shifterLoaded.getReferencedShape().getArea());
assertTrue(shifterLoaded.getReferencedShape() instanceof Rectangle);
assertTrue(shifter.getMainShape().getArea() == shifterLoaded.getMainShape().getArea());
assertEquals(shifter.getAvailableShapes().size(), shifterLoaded.getAvailableShapes().size());
}
use of com.mongodb.BasicDBObject in project morphia by mongodb.
the class AggregationTest method testDateAggregation.
@Test
public void testDateAggregation() {
AggregationPipeline pipeline = getDs().createAggregation(User.class).group(id(grouping("month", accumulator("$month", "date")), grouping("year", accumulator("$year", "date"))), grouping("count", accumulator("$sum", 1)));
final DBObject group = ((AggregationPipelineImpl) pipeline).getStages().get(0);
final DBObject id = getDBObject(group, "$group", "_id");
Assert.assertEquals(new BasicDBObject("$month", "$date"), id.get("month"));
Assert.assertEquals(new BasicDBObject("$year", "$date"), id.get("year"));
pipeline.aggregate(User.class);
}
use of com.mongodb.BasicDBObject in project morphia by mongodb.
the class TestMapping method testMaps.
@Test
public void testMaps() throws Exception {
final DBCollection articles = getDb().getCollection("articles");
getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
final Article related = new Article();
final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
articles.save(relatedDbObj);
final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final Article article = new Article();
article.setTranslation("en", new Translation("Hello World", "Just a test"));
article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
article.setAttribute("myDate", new Date());
article.setAttribute("myString", "Test");
article.setAttribute("myInt", 123);
article.putRelated("test", relatedLoaded);
final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
articles.save(articleDbObj);
final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
Aggregations