Search in sources :

Example 91 with DBObject

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

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

the class TestMapping method testEmbeddedEntityDBObjectHasNoClassname.

@Test
public void testEmbeddedEntityDBObjectHasNoClassname() throws Exception {
    getMorphia().map(ContainsEmbeddedEntity.class);
    final ContainsEmbeddedEntity cee = new ContainsEmbeddedEntity();
    cee.cil = new ContainsIntegerList();
    cee.cil.intList = Collections.singletonList(1);
    final DBObject dbObj = getMorphia().toDBObject(cee);
    assertTrue(!((DBObject) dbObj.get("cil")).containsField(Mapper.CLASS_NAME_FIELDNAME));
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 93 with DBObject

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

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

the class EmbeddedReferenceType method testQueryFormat.

@Test
@SuppressWarnings("deprecation")
public void testQueryFormat() {
    Assume.assumeTrue("This test requires Java 8", JAVA_8);
    Query<ReferenceType> query = getDs().find(ReferenceType.class).field("id").equal(new ObjectId(0, 0, (short) 0, 0)).field("referenceType").equal(new ReferenceType(2, "far")).field("embeddedType").equal(new EmbeddedReferenceType(3, "strikes")).field("string").equal("some value").field("embeddedArray").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("embeddedSet").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("embeddedList").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("map.bar").equal(new EmbeddedReferenceType(1, "chance")).field("mapOfList.bar").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mapOfList.foo").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 1).filter("text", "chance")).field("selfReference").equal(new ReferenceType(1, "blah")).field("mixedTypeList").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("mixedTypeList").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mixedTypeMap.foo").equal(new ReferenceType(3, "strikes")).field("mixedTypeMap.bar").equal(new EmbeddedReferenceType(3, "strikes")).field("mixedTypeMapOfList.bar").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mixedTypeMapOfList.foo").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("referenceMap.foo").equal(new ReferenceType(1, "chance")).field("referenceMap.bar").equal(new EmbeddedReferenceType(1, "chance"));
    DBObject dbObject = ((QueryImpl) query).getQueryObject();
    Assert.assertEquals(BasicDBObject.parse(readFully("/QueryStructure.json")), dbObject);
}
Also used : QueryImpl(org.mongodb.morphia.query.QueryImpl) ObjectId(org.bson.types.ObjectId) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 95 with DBObject

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

the class AggregationTest method getDBObject.

private DBObject getDBObject(final DBObject dbObject, final String... path) {
    DBObject current = dbObject;
    for (String step : path) {
        Object next = current.get(step);
        Assert.assertNotNull(format("Could not find %s in \n%s", step, current), next);
        current = (DBObject) next;
    }
    return current;
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

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