Search in sources :

Example 86 with BasicDBObject

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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) PathTarget(org.mongodb.morphia.internal.PathTarget)

Example 87 with BasicDBObject

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());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 88 with BasicDBObject

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ShapeShifter(org.mongodb.morphia.testmodel.ShapeShifter) Test(org.junit.Test)

Example 89 with BasicDBObject

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 90 with BasicDBObject

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Translation(org.mongodb.morphia.testmodel.Translation) Article(org.mongodb.morphia.testmodel.Article) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Date(java.util.Date) Test(org.junit.Test)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)566 DBObject (com.mongodb.DBObject)338 Test (org.junit.Test)175 DBCollection (com.mongodb.DBCollection)77 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)68 ApiOperation (io.swagger.annotations.ApiOperation)65 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)64 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)54 ArrayList (java.util.ArrayList)53 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)52 ObjectId (org.bson.types.ObjectId)43 DBCursor (com.mongodb.DBCursor)41 HashMap (java.util.HashMap)35 BasicDBList (com.mongodb.BasicDBList)32 List (java.util.List)30 Map (java.util.Map)26 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)18