Example usage for com.mongodb BasicDBObject putAll

List of usage examples for com.mongodb BasicDBObject putAll

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject putAll.

Prototype

@SuppressWarnings("unchecked")
    @Override
    public void putAll(final Map m) 

Source Link

Usage

From source file:org.springframework.data.mongodb.core.aggregation.GroupOperation.java

License:Apache License

@Override
public com.mongodb.DBObject toDBObject(AggregationOperationContext context) {

    BasicDBObject operationObject = new BasicDBObject();

    if (idFields.exposesNoNonSyntheticFields()) {

        operationObject.put(Fields.UNDERSCORE_ID, null);

    } else if (idFields.exposesSingleNonSyntheticFieldOnly()) {

        FieldReference reference = context.getReference(idFields.iterator().next());
        operationObject.put(Fields.UNDERSCORE_ID, reference.toString());

    } else {/*from   ww  w . j  a v a  2 s. co m*/

        BasicDBObject inner = new BasicDBObject();

        for (ExposedField field : idFields) {
            FieldReference reference = context.getReference(field);
            inner.put(field.getName(), reference.toString());
        }

        operationObject.put(Fields.UNDERSCORE_ID, inner);
    }

    for (Operation operation : operations) {
        operationObject.putAll(operation.toDBObject(context));
    }

    return new BasicDBObject("$group", operationObject);
}

From source file:org.springframework.data.mongodb.core.aggregation.ProjectionOperation.java

License:Apache License

@Override
public DBObject toDBObject(AggregationOperationContext context) {

    BasicDBObject fieldObject = new BasicDBObject();

    for (Projection projection : projections) {
        fieldObject.putAll(projection.toDBObject(context));
    }//from   ww  w .jav a 2  s .  co  m

    return new BasicDBObject("$project", fieldObject);
}

From source file:org.springframework.data.mongodb.core.index.CompoundIndexDefinition.java

License:Apache License

@Override
public DBObject getIndexKeys() {

    BasicDBObject dbo = new BasicDBObject();
    dbo.putAll(this.keys);
    dbo.putAll(super.getIndexKeys());
    return dbo;/*  w w  w . j  a va  2 s .  c o  m*/
}

From source file:org.springframework.data.mongodb.core.mapreduce.MapReduceOptions.java

License:Apache License

public DBObject getOptionsObject() {
    BasicDBObject cmd = new BasicDBObject();

    if (verbose != null) {
        cmd.put("verbose", verbose);
    }/*from  www  .  j a v a  2  s . com*/

    cmd.put("out", createOutObject());

    if (finalizeFunction != null) {
        cmd.put("finalize", finalizeFunction);
    }

    if (scopeVariables != null) {
        cmd.put("scope", scopeVariables);
    }

    if (limit != null) {
        cmd.put("limit", limit);
    }

    if (!extraOptions.keySet().isEmpty()) {
        cmd.putAll(extraOptions);
    }

    return cmd;
}

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

License:Apache License

@SuppressWarnings("unchecked")
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName) {

    if (near == null) {
        throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
    }/*  w ww. j a v a 2  s . c om*/

    if (entityClass == null) {
        throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
    }

    String collection = StringUtils.hasText(collectionName) ? collectionName
            : determineCollectionName(entityClass);
    DBObject nearDbObject = near.toDBObject();

    BasicDBObject command = new BasicDBObject("geoNear", collection);
    command.putAll(nearDbObject);

    if (nearDbObject.containsField("query")) {
        DBObject query = (DBObject) nearDbObject.get("query");
        command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}",
                serializeToJsonSafely(command), entityClass, collectionName);
    }

    CommandResult commandResult = executeCommand(command, this.readPreference);
    List<Object> results = (List<Object>) commandResult.get("results");
    results = results == null ? Collections.emptyList() : results;

    DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(
            new ReadDbObjectCallback<T>(mongoConverter, entityClass, collectionName), near.getMetric());
    List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());

    int index = 0;
    int elementsToSkip = near.getSkip() != null ? near.getSkip() : 0;

    for (Object element : results) {

        /*
         * As MongoDB currently (2.4.4) doesn't support the skipping of elements in near queries
         * we skip the elements ourselves to avoid at least the document 2 object mapping overhead.
         * 
         * @see https://jira.mongodb.org/browse/SERVER-3925
         */
        if (index >= elementsToSkip) {
            result.add(callback.doWith((DBObject) element));
        }
        index++;
    }

    if (elementsToSkip > 0) {
        // as we skipped some elements we have to calculate the averageDistance ourselves:
        return new GeoResults<T>(result, near.getMetric());
    }

    GeoCommandStatistics stats = GeoCommandStatistics.from(commandResult);
    return new GeoResults<T>(result, new Distance(stats.getAverageDistance(), near.getMetric()));
}

From source file:org.springframework.data.mongodb.core.query.BasicQuery.java

License:Apache License

@Override
public DBObject getSortObject() {

    BasicDBObject result = new BasicDBObject();
    if (sortObject != null) {
        result.putAll(sortObject);
    }//from w  w w  .  ja  va 2s. c  o m

    DBObject overrides = super.getSortObject();
    if (overrides != null) {
        result.putAll(overrides);
    }

    return result;
}

From source file:v7db.files.mongodb.V7GridFS.java

License:Open Source License

public ObjectId addFile(ContentPointer data, Object parentFileId, String filename, String contentType)
        throws IOException {
    if (data == null)
        return addFile(null, 0, 0, parentFileId, filename, contentType);

    ObjectId fileId = new ObjectId();
    BasicDBObject metaData = new BasicDBObject("parent", parentFileId).append("_id", fileId);

    metaData.putAll(storage.updateBackRefs(data, fileId, filename, contentType));

    insertMetaData(metaData);/*w  w  w. ja  v  a 2 s.c  o m*/
    return fileId;
}

From source file:v7db.files.mongodb.V7GridFS.java

License:Open Source License

public ObjectId addFile(byte[] data, int offset, int len, Object parentFileId, String filename,
        String contentType) throws IOException {

    ObjectId fileId = new ObjectId();
    BasicDBObject metaData = new BasicDBObject("parent", parentFileId).append("_id", fileId);

    metaData.putAll(
            storage.inlineOrInsertContentsAndBackRefs(100, data, offset, len, fileId, filename, contentType));

    insertMetaData(metaData);/*w  w w .  j  ava2  s  . co m*/
    return fileId;
}

From source file:v7db.files.mongodb.V7GridFS.java

License:Open Source License

/**
 * will close the InputStream before returning
 *//*  ww  w  .j a  va 2s. c o  m*/
public ObjectId addFile(InputStream data, Object parentFileId, String filename, String contentType)
        throws IOException {
    ObjectId fileId = new ObjectId();
    BasicDBObject metaData = new BasicDBObject("parent", parentFileId).append("_id", fileId);

    metaData.putAll(storage.insertContentsAndBackRefs(data, fileId, filename, contentType));

    insertMetaData(metaData);
    return fileId;
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * deletes the object without checking for conflicts. An existing version is
 * moved to the shadow collection, along with a dummy version to mark the
 * deletion. This dummy version can contain optional meta-data (such as who
 * deleted the object, and when).//from www  . j ava  2s .  c o  m
 */
static DBObject remove(DBCollection collection, Object id, BSONObject metaData) {
    DBObject base = collection.findOne(new BasicDBObject("_id", id));
    if (base == null)
        return null;

    // copy to shadow
    DBCollection shadow = getShadowCollection(collection);
    int version = getVersion(base);
    BasicDBObject revId = new BasicDBObject("_id", getId(base)).append(_VERSION, version);
    base.put("_id", revId);
    WriteResult r = shadow.insert(base, WriteConcern.SAFE);

    // TODO: if already there, no error
    r.getLastError().throwOnError();

    // add the dummy version
    BasicDBObject dummy = new BasicDBObject("_id", revId.append(_VERSION, version + 1)).append(_VERSION,
            "deleted:" + (version + 1));
    if (metaData != null)
        dummy.putAll(metaData);
    r = shadow.insert(dummy, WriteConcern.SAFE);
    // TODO: if already there, no error
    r.getLastError().throwOnError();

    collection.remove(new BasicDBObject("_id", id));
    return base;

}