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