Search in sources :

Example 91 with BasicDBObject

use of com.mongodb.BasicDBObject in project morphia by mongodb.

the class TestMapping method testRecursiveReference.

@Test
public void testRecursiveReference() throws Exception {
    final DBCollection stuff = getDb().getCollection("stuff");
    getMorphia().map(RecursiveParent.class).map(RecursiveChild.class);
    final RecursiveParent parent = new RecursiveParent();
    final DBObject parentDbObj = getMorphia().toDBObject(parent);
    stuff.save(parentDbObj);
    final RecursiveChild child = new RecursiveChild();
    final DBObject childDbObj = getMorphia().toDBObject(child);
    stuff.save(childDbObj);
    final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    parentLoaded.setChild(childLoaded);
    childLoaded.setParent(parentLoaded);
    stuff.save(getMorphia().toDBObject(parentLoaded));
    stuff.save(getMorphia().toDBObject(childLoaded));
    final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertNotNull(finalParentLoaded.getChild());
    assertNotNull(finalChildLoaded.getParent());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecursiveChild(org.mongodb.morphia.testmodel.RecursiveChild) RecursiveParent(org.mongodb.morphia.testmodel.RecursiveParent) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 92 with BasicDBObject

use of com.mongodb.BasicDBObject in project morphia by mongodb.

the class TestSuperDatastore method testDeleteWillRemoveAnyDocumentWithAMatchingId.

@Test
public void testDeleteWillRemoveAnyDocumentWithAMatchingId() throws Exception {
    // given
    final String ns = "someCollectionName";
    getDb().getCollection(ns).remove(new BasicDBObject());
    final Rectangle rect = new Rectangle(10, 10);
    ObjectId rectangleId = new ObjectId();
    rect.setId(rectangleId);
    getAds().save(ns, rect);
    final Circle circle = new Circle();
    circle.setId(new ObjectId());
    getAds().save(ns, circle);
    assertEquals(2, getAds().getCount(ns));
    // when
    getAds().delete(ns, Circle.class, rectangleId);
    // then
    assertEquals(1, getAds().getCount(ns));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 93 with BasicDBObject

use of com.mongodb.BasicDBObject in project morphia by mongodb.

the class TestSuperDatastore method testGet.

@Test
public void testGet() throws Exception {
    final String ns = "hotels";
    final Rectangle rect = new Rectangle(10, 10);
    getDb().getCollection(ns).remove(new BasicDBObject());
    getAds().save(ns, rect);
    assertEquals(1, getAds().getCount(ns));
    final Rectangle rectLoaded = getAds().get(ns, Rectangle.class, rect.getId());
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 94 with BasicDBObject

use of com.mongodb.BasicDBObject in project morphia by mongodb.

the class TestSuperDatastore method testFind.

@Test
public void testFind() throws Exception {
    final String ns = "hotels";
    Rectangle rect = new Rectangle(10, 10);
    ObjectId id = new ObjectId();
    rect.setId(id);
    getDb().getCollection(ns).remove(new BasicDBObject());
    getAds().save(ns, rect);
    assertEquals(1, getAds().getCount(ns));
    Rectangle rectLoaded = getAds().find(ns, Rectangle.class).get();
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
    rect = new Rectangle(2, 1);
    //saved to default collection name (kind)
    getAds().save(rect);
    assertEquals(1, getAds().getCount(rect));
    rect.setId(null);
    //saved to default collection name (kind)
    getAds().save(rect);
    assertEquals(2, getAds().getCount(rect));
    rect = new Rectangle(4, 3);
    getAds().save(ns, rect);
    assertEquals(2, getAds().getCount(ns));
    rectLoaded = getAds().find(ns, Rectangle.class).asList().get(1);
    assertEquals(rect.getId(), rectLoaded.getId());
    assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
    getAds().find(ns, Rectangle.class, "_id !=", "-1", 1, 1).get();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 95 with BasicDBObject

use of com.mongodb.BasicDBObject in project morphia by mongodb.

the class GeoEntitiesTest method shouldSaveAnEntityWithAGeoCollectionType.

@Test
public void shouldSaveAnEntityWithAGeoCollectionType() {
    // given
    String name = "What, everything?";
    Point point = point(3.0, 7.0);
    LineString lineString = lineString(point(1, 2), point(3, 5), point(19, 13));
    Polygon polygonWithHoles = polygon(lineString(point(1.1, 2.0), point(2.3, 3.5), point(3.7, 1.0), point(1.1, 2.0)), lineString(point(1.5, 2.0), point(1.9, 2.0), point(1.9, 1.8), point(1.5, 2.0)), lineString(point(2.2, 2.1), point(2.4, 1.9), point(2.4, 1.7), point(2.1, 1.8), point(2.2, 2.1)));
    MultiPoint multiPoint = GeoJson.multiPoint(point(1, 2), point(3, 5), point(19, 13));
    MultiLineString multiLineString = GeoJson.multiLineString(lineString(point(1, 2), point(3, 5), point(19, 13)), lineString(point(1.5, 2.0), point(1.9, 2.0), point(1.9, 1.8), point(1.5, 2.0)));
    MultiPolygon multiPolygon = multiPolygon(polygon(point(1.1, 2.0), point(2.3, 3.5), point(3.7, 1.0), point(1.1, 2.0)), polygon(lineString(point(1.2, 3.0), point(2.5, 4.5), point(6.7, 1.9), point(1.2, 3.0)), lineString(point(3.5, 2.4), point(1.7, 2.8), point(3.5, 2.4))));
    GeometryCollection geometryCollection = GeoJson.geometryCollection(point, lineString, polygonWithHoles, multiPoint, multiLineString, multiPolygon);
    AllTheThings allTheThings = new AllTheThings(name, geometryCollection);
    // when
    getDs().save(allTheThings);
    // then use the underlying driver to ensure it was persisted correctly to the database
    DBObject storedArea = getDs().getCollection(AllTheThings.class).findOne(new BasicDBObject("name", name), new BasicDBObject("_id", 0).append("className", 0));
    assertThat(storedArea, is(notNullValue()));
    assertThat(storedArea.toString(), JSONMatcher.jsonEqual("  {" + " name: '" + name + "'," + " everything: " + " {" + "  type: 'GeometryCollection', " + "  geometries: " + "  [" + "    {" + "     type: 'Point', " + "     coordinates: [7.0, 3.0]" + "    }, " + "    {" + "     type: 'LineString', " + "     coordinates: [ [ 2.0,  1.0]," + "                    [ 5.0,  3.0]," + "                    [13.0, 19.0] ]" + "    }," + "    {" + "     type: 'Polygon', " + "     coordinates: " + "       [ [ [ 2.0, 1.1]," + "           [ 3.5, 2.3]," + "           [ 1.0, 3.7]," + "           [ 2.0, 1.1] " + "         ]," + "         [ [ 2.0, 1.5]," + "           [ 2.0, 1.9]," + "           [ 1.8, 1.9]," + "           [ 2.0, 1.5] " + "         ]," + "         [ [ 2.1, 2.2]," + "           [ 1.9, 2.4]," + "           [ 1.7, 2.4]," + "           [ 1.8, 2.1]," + "           [ 2.1, 2.2] " + "         ]" + "       ]" + "    }," + "    {" + "     type: 'MultiPoint', " + "     coordinates: [ [ 2.0,  1.0]," + "                    [ 5.0,  3.0]," + "                    [13.0, 19.0] ]" + "    }," + "    {" + "     type: 'MultiLineString', " + "     coordinates: " + "        [ [ [ 2.0,  1.0]," + "            [ 5.0,  3.0]," + "            [13.0, 19.0] " + "          ], " + "          [ [ 2.0, 1.5]," + "            [ 2.0, 1.9]," + "            [ 1.8, 1.9]," + "            [ 2.0, 1.5] " + "          ]" + "        ]" + "    }," + "    {" + "     type: 'MultiPolygon', " + "     coordinates: [ [ [ [ 2.0, 1.1]," + "                        [ 3.5, 2.3]," + "                        [ 1.0, 3.7]," + "                        [ 2.0, 1.1]," + "                      ]" + "                    ]," + "                    [ [ [ 3.0, 1.2]," + "                        [ 4.5, 2.5]," + "                        [ 1.9, 6.7]," + "                        [ 3.0, 1.2] " + "                      ]," + "                      [ [ 2.4, 3.5]," + "                        [ 2.8, 1.7]," + "                        [ 2.4, 3.5] " + "                      ]," + "                    ]" + "                  ]" + "    }" + "  ]" + " }" + "}"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) GeoJson.lineString(org.mongodb.morphia.geo.GeoJson.lineString) GeoJson.multiPolygon(org.mongodb.morphia.geo.GeoJson.multiPolygon) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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