Search in sources :

Example 86 with DBObject

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

the class QueryImpl method getFieldsObject.

@Override
@Deprecated
public DBObject getFieldsObject() {
    DBObject projection = getOptions().getProjection();
    if (projection == null || projection.keySet().size() == 0) {
        return null;
    }
    final MappedClass mc = ds.getMapper().getMappedClass(clazz);
    Entity entityAnnotation = mc.getEntityAnnotation();
    final BasicDBObject fieldsFilter = copy(projection);
    if (includeFields && entityAnnotation != null && !entityAnnotation.noClassnameStored()) {
        fieldsFilter.put(Mapper.CLASS_NAME_FIELDNAME, 1);
    }
    return fieldsFilter;
}
Also used : Entity(org.mongodb.morphia.annotations.Entity) BasicDBObject(com.mongodb.BasicDBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 87 with DBObject

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

the class GeoFieldCriteria method addTo.

@Override
public void addTo(final DBObject obj) {
    final BasicDBObjectBuilder query;
    switch(getOperator()) {
        case NEAR:
            query = BasicDBObjectBuilder.start(FilterOperator.NEAR.val(), getValue());
            break;
        case NEAR_SPHERE:
            query = BasicDBObjectBuilder.start(FilterOperator.NEAR_SPHERE.val(), getValue());
            break;
        case WITHIN_BOX:
            query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
            break;
        case WITHIN_CIRCLE:
            query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
            break;
        case WITHIN_CIRCLE_SPHERE:
            query = BasicDBObjectBuilder.start().push(FilterOperator.GEO_WITHIN.val()).add(getOperator().val(), getValue());
            break;
        default:
            throw new UnsupportedOperationException(getOperator() + " not supported for geo-query");
    }
    //add options...
    if (opts != null) {
        for (final Map.Entry<String, Object> e : opts.entrySet()) {
            query.append(e.getKey(), e.getValue());
        }
    }
    obj.put(getField(), query.get());
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) Map(java.util.Map)

Example 88 with DBObject

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

the class TestExpireAfterSeconds method testClassAnnotation.

@Test
public void testClassAnnotation() {
    getMorphia().map(ClassAnnotation.class);
    getDs().ensureIndexes();
    getDs().save(new ClassAnnotation());
    final DB db = getDs().getDB();
    final DBCollection dbCollection = db.getCollection("ClassAnnotation");
    final List<DBObject> indexes = dbCollection.getIndexInfo();
    Assert.assertNotNull(indexes);
    Assert.assertEquals(2, indexes.size());
    DBObject index = null;
    for (final DBObject candidateIndex : indexes) {
        if (candidateIndex.containsField("expireAfterSeconds")) {
            index = candidateIndex;
        }
    }
    Assert.assertNotNull(index);
    Assert.assertTrue(index.containsField("expireAfterSeconds"));
    Assert.assertEquals(5, ((Number) index.get("expireAfterSeconds")).intValue());
}
Also used : DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) DB(com.mongodb.DB) Test(org.junit.Test)

Example 89 with DBObject

use of com.mongodb.DBObject 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 90 with DBObject

use of com.mongodb.DBObject 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)

Aggregations

DBObject (com.mongodb.DBObject)646 BasicDBObject (com.mongodb.BasicDBObject)445 Test (org.junit.Test)267 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)95 DBCollection (com.mongodb.DBCollection)84 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)79 ApiOperation (io.swagger.annotations.ApiOperation)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)70 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)63 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)53 ArrayList (java.util.ArrayList)44 DBCursor (com.mongodb.DBCursor)42 HashMap (java.util.HashMap)40 List (java.util.List)35 ObjectId (org.bson.types.ObjectId)30 BasicDBList (com.mongodb.BasicDBList)29 Map (java.util.Map)28 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)20 BSONObject (org.bson.BSONObject)19