Search in sources :

Example 1 with PathTarget

use of org.mongodb.morphia.internal.PathTarget in project morphia by mongodb.

the class UpdateOpsImpl method push.

@Override
public UpdateOperations<T> push(final String field, final List<?> values, final PushOptions options) {
    if (values == null || values.isEmpty()) {
        throw new QueryException("Values cannot be null or empty.");
    }
    PathTarget pathTarget = new PathTarget(mapper, mapper.getMappedClass(clazz), field);
    if (!validateNames) {
        pathTarget.disableValidation();
    }
    BasicDBObject dbObject = new BasicDBObject(UpdateOperator.EACH.val(), mapper.toMongoObject(pathTarget.getTarget(), null, values));
    options.update(dbObject);
    addOperation(UpdateOperator.PUSH, pathTarget.translatedPath(), dbObject);
    return this;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) PathTarget(org.mongodb.morphia.internal.PathTarget)

Example 2 with PathTarget

use of org.mongodb.morphia.internal.PathTarget in project morphia by mongodb.

the class UpdateOpsImpl method add.

//TODO Clean this up a little.
protected void add(final UpdateOperator op, final String f, final Object value, final boolean convert) {
    if (value == null) {
        throw new QueryException("Val cannot be null");
    }
    Object val = value;
    PathTarget pathTarget = new PathTarget(mapper, mapper.getMappedClass(clazz), f);
    if (!validateNames) {
        pathTarget.disableValidation();
    }
    MappedField mf = pathTarget.getTarget();
    if (convert) {
        if (UpdateOperator.PULL_ALL.equals(op) && value instanceof List) {
            val = toDBObjList(mf, (List<?>) value);
        } else {
            val = mapper.toMongoObject(mf, null, value);
        }
    }
    if (UpdateOperator.ADD_TO_SET_EACH.equals(op)) {
        val = new BasicDBObject(UpdateOperator.EACH.val(), val);
    }
    addOperation(op, pathTarget.translatedPath(), val);
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) BasicDBObject(com.mongodb.BasicDBObject) PathTarget(org.mongodb.morphia.internal.PathTarget) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) List(java.util.List) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)2 PathTarget (org.mongodb.morphia.internal.PathTarget)2 DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 MappedField (org.mongodb.morphia.mapping.MappedField)1