@Override public UpdateOperations<T> max(final String field, final Number value) { add(UpdateOperator.MAX, field, value, false); return this; }
public DBObject getUpdateOperationsObject() { return u_.getOps(); }
@Override public UpdateOperations<T> dec(final String field) { return inc(field, -1); }
public MorphiaUpdateOperations validation(boolean validate) { if (validate) u_.enableValidation(); else u_.disableValidation(); return this; }
@Override public <T> UpdateOperations<T> createUpdateOperations(final Class<T> clazz) { return new UpdateOpsImpl<T>(clazz, getMapper()); }
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); }
public MorphiaUpdateOperations addAll(String fieldExpr, List<?> values, boolean addDups) { u_.addAll(fieldExpr, values, addDups); return this; }
@Override @Deprecated public UpdateOperations<T> add(final String field, final Object value) { return addToSet(field, value); }
public MorphiaUpdateOperations dec(String fieldExpr) { E.invalidArgIf(S.empty(fieldExpr));; if (fieldExpr.startsWith("by")) fieldExpr = fieldExpr.substring(2); String[] keys = fieldExpr.split("(And|[,;\\s]+)"); for (int i = 0; i < keys.length; ++i) { StringBuilder sb = new StringBuilder(keys[i]); sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); u_.dec(sb.toString()); } return this; }
@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; }
@Override @Deprecated public UpdateOperations<T> add(final String field, final Object value, final boolean addDups) { if (value == null) { throw new QueryException("Value cannot be null."); } if (addDups) { List<?> values = value instanceof List ? (List<?>) value : singletonList(value); push(field, values); } else { List<?> values = value instanceof List ? (List<?>) value : singletonList(value); addToSet(field, values); } return this; }
public MorphiaUpdateOperations(Class<? extends Model> clazz) { u_ = new UpdateOpsImpl(clazz, ((DatastoreImpl)ds()).getMapper()); c_ = clazz; }
@Override public UpdateOperations<T> unset(final String field) { add(UpdateOperator.UNSET, field, 1, false); return this; }
@Override public UpdateOperations<T> inc(final String field) { return inc(field, 1); }
@Override public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } final MappedClass mc = getMapper().getMappedClass(query.getEntityClass()); final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class); DBObject queryObject = query.getQueryObject(); if (operations.isIsolated()) { queryObject.put("$isolated", true); } if (!fields.isEmpty()) { operations.inc(fields.get(0).getNameToStore(), 1); } final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps(); if (LOG.isTraceEnabled()) { LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert())); } return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()) .getOptions())); }
@Override public UpdateOperations<T> min(final String field, final Number value) { add(UpdateOperator.MIN, field, value, false); return this; }
@Override public UpdateOperations<T> dec(final String field, final Number value) { if ((value instanceof Long) || (value instanceof Integer)) { return inc(field, (value.longValue() * -1)); } if ((value instanceof Double) || (value instanceof Float)) { return inc(field, (value.doubleValue() * -1)); } throw new IllegalArgumentException( "Currently only the following types are allowed: integer, long, double, float."); }
@Override public <T> T findAndModify(final Query<T> query, final UpdateOperations<T> operations, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.info("Executing findAndModify(" + dbColl.getName() + ") with update "); } updateForVersioning(query, operations); DBObject res = dbColl.findAndModify(query.getQueryObject(), options.copy() .sort(query.getSortObject()) .projection(query.getFieldsObject()) .update(((UpdateOpsImpl<T>) operations).getOps()) .getOptions()); return res == null ? null : mapper.fromDBObject(this, query.getEntityClass(), res, createCache()); }
protected UpdateOperations<T> remove(final String fieldExpr, final boolean firstNotLast) { add(UpdateOperator.POP, fieldExpr, (firstNotLast) ? -1 : 1, false); return this; }