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);
}
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));
}
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());
}
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);
}
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;
}
Aggregations