Example usage for org.springframework.data.mongodb.core.query Query getQueryObject

List of usage examples for org.springframework.data.mongodb.core.query Query getQueryObject

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query Query getQueryObject.

Prototype

public Document getQueryObject() 

Source Link

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName) {
    return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(),
            query.getSortObject(), entityClass);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

private long count(Query query, Class<?> entityClass, String collectionName) {

    Assert.hasText(collectionName);//from  w  w  w. java 2  s .  co m
    final DBObject dbObject = query == null ? null
            : mapper.getMappedObject(query.getQueryObject(),
                    entityClass == null ? null : mappingContext.getPersistentEntity(entityClass));

    return execute(collectionName, new CollectionCallback<Long>() {
        public Long doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            return collection.count(dbObject);
        }
    });
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected WriteResult doUpdate(final String collectionName, final Query query, final Update update,
        final Class<?> entityClass, final boolean upsert, final boolean multi) {

    return execute(collectionName, new CollectionCallback<WriteResult>() {
        public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {

            MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);

            DBObject queryObj = query == null ? new BasicDBObject()
                    : mapper.getMappedObject(query.getQueryObject(), entity);
            DBObject updateObj = update.getUpdateObject();

            for (String key : updateObj.keySet()) {
                updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
            }//www . j  a va 2  s . com

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj
                        + " in collection: " + collectionName);
            }

            WriteResult wr;
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
                    entityClass, updateObj, queryObj);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            if (writeConcernToUse == null) {
                wr = collection.update(queryObj, updateObj, upsert, multi);
            } else {
                wr = collection.update(queryObj, updateObj, upsert, multi, writeConcernToUse);
            }
            handleAnyWriteResultErrors(wr, queryObj, "update with '" + updateObj + "'");
            return wr;
        }
    });
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

protected <T> void doRemove(final String collectionName, final Query query, final Class<T> entityClass) {
    if (query == null) {
        throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null");
    }/*  w  w w. ja  v a  2 s .  c o  m*/
    final DBObject queryObject = query.getQueryObject();
    final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
    execute(collectionName, new CollectionCallback<Void>() {
        public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            DBObject dboq = mapper.getMappedObject(queryObject, entity);
            WriteResult wr = null;
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName,
                    entityClass, null, queryObject);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("remove using query: " + dboq + " in collection: " + collection.getName());
            }
            if (writeConcernToUse == null) {
                wr = collection.remove(dboq);
            } else {
                wr = collection.remove(dboq, writeConcernToUse);
            }
            handleAnyWriteResultErrors(wr, dboq, "remove");
            return null;
        }
    });
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

private DBObject copyQuery(Query query, DBObject copyMapReduceOptions) {
    if (query != null) {
        if (query.getSkip() != 0 || query.getFieldsObject() != null) {
            throw new InvalidDataAccessApiUsageException(
                    "Can not use skip or field specification with map reduce operations");
        }/* ww w .j  a v  a  2  s  .co  m*/
        if (query.getQueryObject() != null) {
            copyMapReduceOptions.put("query", query.getQueryObject());
        }
        if (query.getLimit() > 0) {
            copyMapReduceOptions.put("limit", query.getLimit());
        }
        if (query.getSortObject() != null) {
            copyMapReduceOptions.put("sort", query.getSortObject());
        }
    }
    return copyMapReduceOptions;
}

From source file:org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.java

@Override
protected Query createQuery(ConvertingParameterAccessor accessor) {

    String queryString = replacePlaceholders(query, accessor);

    Query query = null;

    if (fieldSpec != null) {
        String fieldString = replacePlaceholders(fieldSpec, accessor);
        query = new BasicQuery(queryString, fieldString);
    } else {/* w  ww .  j a  v a 2  s  .  co m*/
        query = new BasicQuery(queryString);
    }

    QueryUtils.applySorting(query, accessor.getSort());

    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Created query %s", query.getQueryObject()));
    }

    return query;
}