Example usage for com.mongodb DBObject putAll

List of usage examples for com.mongodb DBObject putAll

Introduction

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

Prototype

void putAll(BSONObject o);

Source Link

Document

Sets all key/value pairs from an object into this object

Usage

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

License:Apache License

@Override
public DBObject getFieldsObject() {

    if (fieldsObject == null) {
        return super.getFieldsObject();
    }/*from w  ww .java  2  s.  c o  m*/

    if (super.getFieldsObject() != null) {

        DBObject combinedFieldsObject = new BasicDBObject();
        combinedFieldsObject.putAll(fieldsObject);
        combinedFieldsObject.putAll(super.getFieldsObject());
        return combinedFieldsObject;
    }

    return fieldsObject;
}

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

License:Apache License

protected DBObject getSingleCriteriaObject() {

    DBObject dbo = new BasicDBObject();
    boolean not = false;

    for (Entry<String, Object> entry : criteria.entrySet()) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (requiresGeoJsonFormat(value)) {
            value = new BasicDBObject("$geometry", value);
        }//from  ww w.j  a  va2s . c  o  m

        if (not) {
            DBObject notDbo = new BasicDBObject();
            notDbo.put(key, value);
            dbo.put("$not", notDbo);
            not = false;
        } else {
            if ("$not".equals(key) && value == null) {
                not = true;
            } else {
                dbo.put(key, value);
            }
        }
    }

    if (!StringUtils.hasText(this.key)) {
        if (not) {
            return new BasicDBObject("$not", dbo);
        }
        return dbo;
    }

    DBObject queryCriteria = new BasicDBObject();

    if (!NOT_SET.equals(isValue)) {
        queryCriteria.put(this.key, this.isValue);
        queryCriteria.putAll(dbo);
    } else {
        queryCriteria.put(this.key, dbo);
    }

    return queryCriteria;
}

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

License:Apache License

public DBObject getQueryObject() {

    DBObject dbo = new BasicDBObject();

    for (CriteriaDefinition definition : criteria.values()) {
        dbo.putAll(definition.getCriteriaObject());
    }/*ww  w .  ja v  a 2s  .c  o  m*/

    if (!restrictedTypes.isEmpty()) {
        dbo.put(RESTRICTED_TYPES_KEY, getRestrictedTypes());
    }

    return dbo;
}

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

License:Apache License

@Override
public DBObject getSortObject() {

    DBObject sort = new BasicDBObject();

    if (this.sortByScore) {
        sort.put(getScoreFieldName(), META_TEXT_SCORE);
    }/*from w w  w  . ja  va2  s.  c  o  m*/

    if (super.getSortObject() != null) {
        sort.putAll(super.getSortObject());
    }

    return sort;
}

From source file:org.springframework.data.mongodb.crossstore.MongoChangeSetPersister.java

License:Apache License

public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
    if (cs == null) {
        log.debug("Flush: changeset was null, nothing to flush.");
        return 0L;
    }/*from   w w  w  .j a  va 2s.co  m*/

    if (log.isDebugEnabled()) {
        log.debug("Flush: changeset: {}", cs.getValues());
    }

    String collName = getCollectionNameForEntity(entity.getClass());
    if (mongoTemplate.getCollection(collName) == null) {
        mongoTemplate.createCollection(collName);
    }

    for (String key : cs.getValues().keySet()) {
        if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) {
            Object value = cs.getValues().get(key);
            final DBObject dbQuery = new BasicDBObject();
            dbQuery.put(ENTITY_ID, getPersistentId(entity, cs));
            dbQuery.put(ENTITY_CLASS, entity.getClass().getName());
            dbQuery.put(ENTITY_FIELD_NAME, key);
            DBObject dbId = mongoTemplate.execute(collName, new CollectionCallback<DBObject>() {
                public DBObject doInCollection(DBCollection collection)
                        throws MongoException, DataAccessException {
                    return collection.findOne(dbQuery);
                }
            });
            if (value == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Flush: removing: {}", dbQuery);
                }
                mongoTemplate.execute(collName, new CollectionCallback<Object>() {
                    public Object doInCollection(DBCollection collection)
                            throws MongoException, DataAccessException {
                        collection.remove(dbQuery);
                        return null;
                    }
                });
            } else {
                final DBObject dbDoc = new BasicDBObject();
                dbDoc.putAll(dbQuery);
                if (log.isDebugEnabled()) {
                    log.debug("Flush: saving: {}", dbQuery);
                }
                mongoTemplate.getConverter().write(value, dbDoc);
                dbDoc.put(ENTITY_FIELD_CLASS, value.getClass().getName());
                if (dbId != null) {
                    dbDoc.put("_id", dbId.get("_id"));
                }
                mongoTemplate.execute(collName, new CollectionCallback<Object>() {
                    public Object doInCollection(DBCollection collection)
                            throws MongoException, DataAccessException {
                        collection.save(dbDoc);
                        return null;
                    }
                });
            }
        }
    }
    return 0L;
}

From source file:org.springframework.datastore.mapping.mongo.MongoDatastore.java

License:Apache License

/**
 * Indexes any properties that are mapped with index:true
 * @param entity The entity/*from   ww  w  . ja va  2  s  .  co m*/
 * @param template The template
 */
protected void initializeIndices(final PersistentEntity entity, final MongoTemplate template) {
    template.execute(new DbCallback<Object>() {
        public Object doInDB(DB db) throws MongoException, DataAccessException {
            final DBCollection collection = db.getCollection(template.getDefaultCollectionName());

            final ClassMapping<MongoCollection> classMapping = entity.getMapping();
            if (classMapping != null) {
                final MongoCollection mappedForm = classMapping.getMappedForm();
                if (mappedForm != null) {
                    for (Map compoundIndex : mappedForm.getCompoundIndices()) {
                        DBObject indexDef = new BasicDBObject(compoundIndex);
                        collection.ensureIndex(indexDef);
                    }

                }
            }

            for (PersistentProperty<MongoAttribute> property : entity.getPersistentProperties()) {
                final boolean indexed = isIndexed(property);

                if (indexed) {

                    final MongoAttribute mongoAttributeMapping = property.getMapping().getMappedForm();

                    DBObject dbObject = new BasicDBObject();
                    final String fieldName = getMongoFieldNameForProperty(property);
                    dbObject.put(fieldName, 1);
                    DBObject options = new BasicDBObject();
                    if (mongoAttributeMapping != null) {
                        final Map attributes = mongoAttributeMapping.getIndexAttributes();
                        if (attributes != null) {
                            if (attributes.containsKey(MongoAttribute.INDEX_TYPE)) {
                                dbObject.put(fieldName, attributes.remove(MongoAttribute.INDEX_TYPE));
                            }
                            options.putAll(attributes);
                        }
                    }
                    if (options.toMap().isEmpty()) {
                        collection.ensureIndex(dbObject);
                    } else {
                        collection.ensureIndex(dbObject, options);
                    }
                }
            }

            return null;
        }

        String getMongoFieldNameForProperty(PersistentProperty<MongoAttribute> property) {
            PropertyMapping<MongoAttribute> pm = property.getMapping();
            String propKey = null;
            if (pm.getMappedForm() != null) {
                propKey = pm.getMappedForm().getField();
            }
            if (propKey == null) {
                propKey = property.getName();
            }
            return propKey;
        }
    });
}

From source file:org.tylproject.vaadin.addon.MongoContainer.java

License:Apache License

/**
 * @return a cursor with the given optional params
 *///from w w  w . ja v a2  s.  co m
protected DBCursor cursor(DBObject additionalCriteria) {
    final Query q = this.query;
    DBObject criteriaObject = q.getQueryObject();
    if (additionalCriteria != null) {
        criteriaObject.putAll(additionalCriteria);
    }

    DBObject projectionObject = new BasicDBObject(ID, true);

    String collectionName = mongoOps.getCollectionName(beanClass);
    DBCollection dbCollection = mongoOps.getCollection(collectionName);

    // TODO: keep cursor around to possibly reuse
    DBCursor cursor = dbCollection.find(criteriaObject, projectionObject);

    if (this.baseSort != null || this.sort != null) {
        DBObject sortObject = q.getSortObject();
        cursor.sort(sortObject);
    }

    return cursor;
}

From source file:sam.com.training.model.util.PersistenceUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static DBObject fromModelObjectToDBObject(Object aMember) throws PersistenceException {

    // create ObjectMapper instance
    ObjectMapper objectMapper = new ObjectMapper();

    try {// w w w  .  ja va  2 s  .  c om
        StringWriter jSonString = new StringWriter();
        objectMapper.writeValue(jSonString, aMember);
        Map<String, String> allContentMap = new HashMap<String, String>();

        DBObject aDBObject = new BasicDBObject();
        allContentMap = objectMapper.readValue(jSonString.toString().getBytes(), Map.class);
        aDBObject.putAll(allContentMap);
        return aDBObject;
    } catch (JsonParseException jSonEx) {
        jSonEx.printStackTrace();
        throw new PersistenceException(jSonEx);
    } catch (JsonMappingException jSonMappingEx) {
        jSonMappingEx.printStackTrace();
        throw new PersistenceException(jSonMappingEx);
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
        throw new PersistenceException(ioEx);
    }
}

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

License:Open Source License

void updateContents(DBObject metaData, ContentPointer newContents) throws IOException {
    ContentPointer oldContents = getContentPointer(metaData);

    if (newContents.contentEquals(oldContents))
        return;/*from w  w  w. ja va 2  s  .c  o m*/

    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    Object fileId = metaData.get("_id");

    BSONObject newContent = storage.updateBackRefs(newContents, fileId, filename, contentType);

    metaData.removeField("sha");
    metaData.removeField("length");
    metaData.removeField("in");

    metaData.putAll(newContent);

    updateMetaData(metaData);
}

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

License:Open Source License

void insertContents(DBObject metaData, ContentPointer newContents) throws IOException {

    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    Object fileId = metaData.get("_id");

    if (newContents != null) {
        BSONObject newContent = storage.updateBackRefs(newContents, fileId, filename, contentType);

        metaData.removeField("sha");
        metaData.removeField("length");
        metaData.removeField("in");

        metaData.putAll(newContent);
    }/*from  w w  w.ja v a 2 s .c  om*/

    insertMetaData(metaData);
}