Search in sources :

Example 56 with DBObject

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

the class DatastoreImpl method updateFirst.

@Override
@Deprecated
public <T> UpdateResults updateFirst(final Query<T> query, final T entity, final boolean createIfMissing) {
    if (getMapper().getMappedClass(entity).getMappedVersionField() != null) {
        throw new UnsupportedOperationException("updateFirst() is not supported with versioned entities");
    }
    final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
    final DBObject dbObj = mapper.toDBObject(entity, involvedObjects);
    final UpdateResults res = update(query, dbObj, createIfMissing, false, getWriteConcern(entity));
    // update _id field
    if (res.getInsertedCount() > 0) {
        dbObj.put(Mapper.ID_KEY, res.getNewId());
    }
    postSaveOperations(singletonList(entity), involvedObjects, getCollection(entity), false);
    return res;
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults) LinkedHashMap(java.util.LinkedHashMap)

Example 57 with DBObject

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

the class DatastoreImpl method findAndModify.

@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());
}
Also used : DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 58 with DBObject

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

the class DatastoreImpl method merge.

@Override
@SuppressWarnings("unchecked")
public <T> Key<T> merge(final T entity, final WriteConcern wc) {
    T unwrapped = entity;
    final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
    final DBObject dbObj = mapper.toDBObject(unwrapped, involvedObjects);
    final Key<T> key = mapper.getKey(unwrapped);
    unwrapped = ProxyHelper.unwrap(unwrapped);
    final Object id = mapper.getId(unwrapped);
    if (id == null) {
        throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
    }
    // remove (immutable) _id field for update.
    final Object idValue = dbObj.get(Mapper.ID_KEY);
    dbObj.removeField(Mapper.ID_KEY);
    WriteResult wr;
    final MappedClass mc = mapper.getMappedClass(unwrapped);
    final DBCollection dbColl = getCollection(unwrapped);
    // try to do an update if there is a @Version field
    wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, new InsertOptions().writeConcern(wc), mc);
    if (wr == null) {
        final Query<T> query = (Query<T>) createQuery(unwrapped.getClass()).filter(Mapper.ID_KEY, id);
        wr = update(query, new BasicDBObject("$set", dbObj), false, false, wc).getWriteResult();
    }
    final UpdateResults res = new UpdateResults(wr);
    if (res.getUpdatedCount() == 0) {
        throw new UpdateException("Nothing updated");
    }
    dbObj.put(Mapper.ID_KEY, idValue);
    postSaveOperations(Collections.<Object>singletonList(entity), involvedObjects, dbColl, false);
    return key;
}
Also used : Query(org.mongodb.morphia.query.Query) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults) LinkedHashMap(java.util.LinkedHashMap) MappingException(org.mongodb.morphia.mapping.MappingException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateException(org.mongodb.morphia.query.UpdateException)

Example 59 with DBObject

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

the class DatastoreImpl method postSaveOperations.

@SuppressWarnings("unchecked")
private <T> List<Key<T>> postSaveOperations(final Iterable<T> entities, final Map<Object, DBObject> involvedObjects, final DBCollection collection, final boolean fetchKeys) {
    List<Key<T>> keys = new ArrayList<Key<T>>();
    for (final T entity : entities) {
        final DBObject dbObj = involvedObjects.remove(entity);
        if (fetchKeys) {
            if (dbObj.get(Mapper.ID_KEY) == null) {
                throw new MappingException(format("Missing _id after save on %s", entity.getClass().getName()));
            }
            mapper.updateKeyAndVersionInfo(this, dbObj, createCache(), entity);
            keys.add(new Key<T>((Class<? extends T>) entity.getClass(), collection.getName(), mapper.getId(entity)));
        }
        mapper.getMappedClass(entity).callLifecycleMethods(PostPersist.class, entity, dbObj, mapper);
    }
    for (Entry<Object, DBObject> entry : involvedObjects.entrySet()) {
        final Object key = entry.getKey();
        mapper.getMappedClass(key).callLifecycleMethods(PostPersist.class, key, entry.getValue(), mapper);
    }
    return keys;
}
Also used : ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 60 with DBObject

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

the class DatastoreImpl method update.

@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()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults)

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