Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.stratio.streaming.functions.SaveToMongoActionExecutionFunction.java

License:Apache License

@Override
public void process(Iterable<StratioStreamingMessage> messages) throws Exception {
    Map<String, BulkWriteOperation> elementsToInsert = new HashMap<String, BulkWriteOperation>();

    for (StratioStreamingMessage event : messages) {
        BasicDBObject object = new BasicDBObject(TIMESTAMP_FIELD, event.getTimestamp());
        for (ColumnNameTypeValue columnNameTypeValue : event.getColumns()) {
            object.append(columnNameTypeValue.getColumn(), columnNameTypeValue.getValue());
        }/* www. j a v  a2  s . c  o m*/

        BulkWriteOperation bulkInsertOperation = elementsToInsert.get(event.getStreamName());

        if (bulkInsertOperation == null) {
            bulkInsertOperation = getDB().getCollection(event.getStreamName())
                    .initializeUnorderedBulkOperation();

            elementsToInsert.put(event.getStreamName(), bulkInsertOperation);
            getDB().getCollection(event.getStreamName()).createIndex(new BasicDBObject(TIMESTAMP_FIELD, -1));
        }

        bulkInsertOperation.insert(object);
    }

    for (Entry<String, BulkWriteOperation> stratioStreamingMessage : elementsToInsert.entrySet()) {
        stratioStreamingMessage.getValue().execute();
    }
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Persists the <code>metric</code> to the account-specific collection as a BasicDBObject.
 *
 * @param metric persisted metric object
 * @return Map containing the collection name as the key and the BasicDBObject representation of the metric as the value
 *//*from w  ww .j a v  a  2s .c om*/
public Map<String, BasicDBObject> writeMetric(SobaMetric metric) {
    DB metricsDB = getDB("nodeablemsgdb");
    String collectionName = Constants.METRIC_COLLECTION_PREFIX + metric.getStream().getAccountId();
    DBCollection metricsCollection = metricsDB.getCollection(collectionName);
    BasicDBObject persistedMetric = new BasicDBObject();

    persistedMetric.append("timestamp", metric.getTimestamp())
            .append("stream",
                    new BasicDBObject().append("accountId", metric.getStream().getAccountId())
                            .append("connectionId", metric.getStream().getConnectionId()).append(
                                    "inventoryItemId", metric.getStream().getInventoryItemId()))
            .append("type", metric.getType().getId().toString()).append("anomaly", metric.isAnomaly())
            .append("granularity", metric.getGranularity().name())
            .append("metricValue", new BasicDBObject().append("mode", metric.getValue().getMode().name())
                    .append("type", metric.getValue().getType().name())
                    .append("value", metric.getValue().getValue())
                    .append("stddev", metric.getValue().getStddev()).append("mean", metric.getValue().getMean())
                    .append("diff", metric.getValue().getDiff()).append("min", metric.getValue().getMin())
                    .append("max", metric.getValue().getMax()));

    metricsCollection.insert(persistedMetric);

    Map<String, BasicDBObject> result = new HashMap<>();
    result.put(collectionName, persistedMetric);
    return result;
}

From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java

License:Open Source License

/**
 * Builds the Json query used both to prepopulate and update an aggregated collection.
 * @param recvTimeTs// w  w w .  jav a2s  .  co m
 * @param entityId
 * @param entityType
 * @param attrName
 * @param attrType
 * @param resolution
 * @return
 */
private BasicDBObject buildQueryForInsertAggregated(GregorianCalendar calendar, String entityId,
        String entityType, String attrName, Resolution resolution) {
    int offset = 0;

    switch (resolution) {
    case SECOND:
        offset = calendar.get(Calendar.SECOND);
        break;
    case MINUTE:
        offset = calendar.get(Calendar.MINUTE);
        break;
    case HOUR:
        offset = calendar.get(Calendar.HOUR_OF_DAY);
        break;
    case DAY:
        offset = calendar.get(Calendar.DAY_OF_MONTH);
        break;
    case MONTH:
        offset = calendar.get(Calendar.MONTH) + 1;
        break;
    default:
        // this should never be reached;
    } // switch

    BasicDBObject query = new BasicDBObject();

    switch (dataModel) {
    case COLLECTIONPERSERVICEPATH:
        query.append("_id",
                new BasicDBObject("entityId", entityId).append("entityType", entityType)
                        .append("attrName", attrName).append("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    case COLLECTIONPERENTITY:
        query.append("_id",
                new BasicDBObject("attrName", attrName).append("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    case COLLECTIONPERATTRIBUTE:
        query.append("_id",
                new BasicDBObject("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    default:
        // this will never be reached
    } // switch

    return query;
}

From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java

License:Open Source License

/**
 * Builds the Json update used when updating an aggregated collection.
 * @param attrType/*from   w w w  .  j ava2 s  .c  o  m*/
 * @param attrValue
 * @return
 */
private BasicDBObject buildUpdateForUpdate(String attrType, double attrValue) {
    BasicDBObject update = new BasicDBObject();
    update.append("$set", new BasicDBObject("attrType", attrType))
            .append("$inc",
                    new BasicDBObject("points.$.samples", 1).append("points.$.sum", attrValue)
                            .append("points.$.sum2", Math.pow(attrValue, 2)))
            .append("$min", new BasicDBObject("points.$.min", attrValue))
            .append("$max", new BasicDBObject("points.$.max", attrValue));
    return update;
}

From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java

License:Open Source License

/**
 * Builds the Json used to prepopulate an aggregated collection.
 * @param attrType//from   www. j  a  v a  2 s  . co  m
 * @param resolution
 * @return
 */
private BasicDBObject buildInsertForPrepopulate(String attrType, Resolution resolution) {
    BasicDBObject update = new BasicDBObject();
    update.append("$setOnInsert",
            new BasicDBObject("attrType", attrType).append("points", buildPrepopulatedPoints(resolution)));
    return update;
}

From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java

License:Open Source License

private BasicDBObject buildUpdateForCollectionHash(String operation, boolean isAggregated, String fiwareService,
        String fiwareServicePath, String entityId, String entityType, String attrName, String destination) {
    BasicDBObject update = new BasicDBObject();
    update.append(operation, new BasicDBObject("dataModel", OrionMongoBaseSink.getStrDataModel(dataModel))
            .append("isAggregated", isAggregated).append("service", fiwareService)
            .append("servicePath", fiwareServicePath).append("entityId", entityId)
            .append("entityType", entityType).append("attrName", attrName).append("destination", destination));
    return update;
}

From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackendImpl.java

License:Open Source License

/**
 * Builds the Json query used both to prepopulate and update an aggregated collection.
 * @param calendar/*from  w  w  w. ja va 2  s .co  m*/
 * @param entityId
 * @param entityType
 * @param attrName
 * @param resolution
 * @return
 */
private BasicDBObject buildQueryForInsertAggregated(GregorianCalendar calendar, String entityId,
        String entityType, String attrName, Resolution resolution) {
    int offset = 0;

    switch (resolution) {
    case SECOND:
        offset = calendar.get(Calendar.SECOND);
        break;
    case MINUTE:
        offset = calendar.get(Calendar.MINUTE);
        break;
    case HOUR:
        offset = calendar.get(Calendar.HOUR_OF_DAY);
        break;
    case DAY:
        offset = calendar.get(Calendar.DAY_OF_MONTH);
        break;
    case MONTH:
        offset = calendar.get(Calendar.MONTH) + 1;
        break;
    default:
        // this should never be reached;
    } // switch

    BasicDBObject query = new BasicDBObject();

    switch (dataModel) {
    case DMBYSERVICEPATH:
        query.append("_id",
                new BasicDBObject("entityId", entityId).append("entityType", entityType)
                        .append("attrName", attrName).append("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    case DMBYENTITY:
        query.append("_id",
                new BasicDBObject("attrName", attrName).append("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    case DMBYATTRIBUTE:
        query.append("_id",
                new BasicDBObject("origin", getOrigin(calendar, resolution))
                        .append("resolution", resolution.toString().toLowerCase())
                        .append("range", getRange(resolution)))
                .append("points.offset", offset);
        break;
    default:
        // this will never be reached
    } // switch

    return query;
}

From source file:com.tengen.Final8.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    DB db = client.getDB("test");
    DBCollection animals = db.getCollection("animals");

    BasicDBObject animal = new BasicDBObject("animal", "monkey");

    animals.insert(animal);//  w  ww.ja va2s.  com
    animal.removeField("animal");
    animal.append("animal", "cat");
    animals.insert(animal);
    animal.removeField("animal");
    animal.append("animal", "lion");
    animals.insert(animal);
}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public void addBill(ObservableList<Part> data, Bill bill) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("Bill");
    System.out.println("Collection user selected successfully");

    LocalDate now = LocalDate.now();
    Date date = java.sql.Date.valueOf(now);

    for (int i = 0; i < data.size(); i++) {

    }/*from   www . j ava2s  .  co m*/

    BasicDBObject doc = new BasicDBObject("title", "Bill").append("Date", date)
            .append("vehicleNo", bill.getVehicleNo()).append("km", bill.getKm())
            .append("payment", bill.getPayment()).append("service", bill.getService())
            .append("total", bill.getTotalCost()).append("partsSize", data.size());

    for (int i = 0; i < data.size(); i++) {
        String parts = "part" + i;
        String quentity = "quant" + i;
        doc.append(parts, data.get(i).getPartNo()).append(quentity, data.get(i).getQuentity());
    }
    coll.insert(doc);
    System.out.println(doc);
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public void addstock(int t, String p) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$set", new BasicDBObject().append("quant", t));
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.append("_id", p);
    coll.update(searchQuery, updateQuery);
    //// w  w  w.  j ava 2 s .c o m

    /*BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$set",
    new BasicDBObject().append("clients", "888"));
            
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.append("type", "vps");
            
    collection.update(searchQuery, updateQuery);*/
}