Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

In this page you can find the example usage for com.mongodb DBCollection insert.

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

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

License:Apache License

/**
 * Takes the information passed in and creates a {@link BasicDBObject} and writes it to the appropriate
 * account-specific inbox./*from  w  ww  .j  av  a2s  .  co m*/
 *
 * @param metricAccount     the metric account
 * @param metricName        the metric name
 * @param metricType        the metric type
 * @param metricTimestamp   the metric timestamp
 * @param metricValue       the metric value
 * @param metricGranularity the metric granularity
 * @param metricCriteria    the metric criteria
 * @param metricAVGY        the metric average/mean
 * @param metricSTDDEV      the metric standard deviation
 * @param metricDIFF        the metric diff
 * @param metricMIN         the metric minimum value seen
 * @param metricMAX         the metric maximum value seen
 * @return Map containing a single entry where the key is the collection the metric was created and the value
 *         being the newly created {@link BasicDBObject}
 */
public Map<String, BasicDBObject> writeMetric(String metricAccount, String metricName, String metricType,
        Long metricTimestamp, Float metricValue, Long metricGranularity, Map<String, String> metricCriteria,
        Float metricAVGY, Float metricSTDDEV, Float metricDIFF, Float metricMIN, Float metricMAX,
        Boolean metricIsAnomaly) {
    DB metricsDB = getDB("nodeablemsgdb");
    String collectionName = Constants.METRIC_COLLECTION_PREFIX + metricAccount;
    DBCollection metricsCollection = metricsDB.getCollection(collectionName);
    BasicDBObject metric = new BasicDBObject();

    metric.put("metricName", metricName);
    metric.put("metricType", metricType);
    metric.put("metricTimestamp", metricTimestamp);
    metric.put("metricValue", metricValue);
    metric.put("metricGranularity", metricGranularity);
    metric.put("metricCriteria", metricCriteria);
    metric.put("metricAVGY", metricAVGY);
    metric.put("metricSTDDEV", metricSTDDEV);
    metric.put("metricDIFF", metricDIFF);
    metric.put("metricMIN", metricMIN);
    metric.put("metricMAX", metricMAX);
    metric.put("metricIsAnomaly", metricIsAnomaly);

    metricsCollection.insert(metric);

    Map<String, BasicDBObject> result = new HashMap<>();

    result.put(collectionName, metric);

    return result;
}

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 w w. j  a  va  2 s. com
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.sube.daos.mongodb.CardMongoDaoImpl.java

License:Apache License

@Override
public void storeSubeCard(SubeCard subeCard) throws InvalidSubeCardException {
    if (!subeCardValidator.isValid(subeCard)) {
        throw new InvalidSubeCardException();
    }/* w w  w.  j  av  a 2s . c  o m*/
    DBCollection collection = getCardCollection();
    collection.ensureIndex(new BasicDBObject("number", 1), new BasicDBObject("unique", true));
    collection.ensureIndex(new BasicDBObject("user.docNum", 1));
    collection.ensureIndex(new BasicDBObject("user.docNum", 1).append("user.docType", 1),
            new BasicDBObject("unique", true));
    DBObject subeCardDBObject = subeCardGenerator.generate(subeCard);
    collection.insert(subeCardDBObject);
    subeCard.setMongoId((ObjectId) subeCardDBObject.get("_id"));
}

From source file:com.sube.daos.mongodb.CardUsagesMongoDaoImpl.java

License:Apache License

private void genericUsageInsertion(SubeCardUsage cardUsage) throws InvalidSubeCardException {
    DBCollection cardUsagesCollection = getCardUsagesCollection();
    cardUsagesCollection.ensureIndex(new BasicDBObject("subeCard", 1));
    cardUsagesCollection.ensureIndex(new BasicDBObject("provider", 1));
    cardUsagesCollection.ensureIndex(new BasicDBObject("datetime", 1));
    cardUsagesCollection.ensureIndex(new BasicDBObject("money", 1));
    DBObject subeCardUsageDBObject = subeCardUsageGenerator.generate(cardUsage);
    cardDao.addToBalance(cardUsage.getCard(), cardUsage.getMoney());
    cardUsagesCollection.insert(subeCardUsageDBObject);
    cardUsage.setMongoId((ObjectId) subeCardUsageDBObject.get("_id"));
}

From source file:com.sube.daos.mongodb.EntryDaoImpl.java

License:Apache License

@Override
public void createDataEntry(DataEntry dataEntry) throws InvalidDataEntryException {
    DBCollection collection = getCollection();
    DBObject dataEntryDBObject = dataEntryGenerator.generate(dataEntry);
    collection.ensureIndex(new BasicDBObject("docNum", 1));
    collection.ensureIndex(new BasicDBObject("docNum", 1).append("docType", 1),
            new BasicDBObject("unique", true));
    collection.insert(dataEntryDBObject);
    dataEntry.setMongoId((ObjectId) dataEntryDBObject.get("_id"));
}

From source file:com.sube.daos.mongodb.GenericProviderDaoImpl.java

License:Apache License

@Override
public void registerProvider(Provider provider) throws InvalidProviderException {
    DBCollection collection = getProvidersCollection();
    collection.ensureIndex(new BasicDBObject("providerName", 1));
    collection.ensureIndex(new BasicDBObject("legalPerson.legalName", 1), new BasicDBObject("unique", true));
    collection.ensureIndex(new BasicDBObject("legalPerson.cuit", 1), new BasicDBObject("unique", true));
    collection.ensureIndex(new BasicDBObject("legalPerson.fantasyName", 1));
    DBObject providerDBObject = providerGenerator.generate(provider);
    collection.insert(providerDBObject);
    provider.setMongoId((ObjectId) providerDBObject.get("_id"));
}

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);
    animal.removeField("animal");
    animal.append("animal", "cat");
    animals.insert(animal);//from  w  ww  .j a  va 2  s.c  o m
    animal.removeField("animal");
    animal.append("animal", "lion");
    animals.insert(animal);
}

From source file:com.terkaly.JavaMongoDB.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*from  w  w w .j  a  v  a  2s.  c  o m*/
        // Create a connection using mongoClient
        // 23.99.88.154 is obtained from the portal
        MongoClient mongoClient = new MongoClient("[ put your ip address here ]", 27017);

        // Get a connection to mydb
        DB db = mongoClient.getDB("mydb");

        // mydb has one or more collections, get "testCollection"
        DBCollection collection = db.getCollection("testCollection");

        // Create an empty object
        BasicDBObject empty = new BasicDBObject();

        // Clear out testCollection
        collection.remove(empty);

        // Acknowledges the write operation only
        // after committing the data to the journal
        mongoClient.setWriteConcern(WriteConcern.JOURNALED);

        // Here is the data format in JSON
        // {
        //   "name": "MongoDB",
        //   "type": "database",
        //   "count": 1,
        //   "info": {
        //         "x": 203,
        //         "y": 102
        //    }
        // }

        BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
                .append("info", new BasicDBObject("x", 203).append("y", 102));
        collection.insert(doc);

        DBObject myDoc = collection.findOne();
        System.out.println(myDoc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public void addCustomer(Customer customer) {
    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("customer");
    System.out.println("Collection user selected successfully");
    BasicDBObject doc = new BasicDBObject("title", "customer").append("name", customer.getName())
            .append("address", customer.getAddress()).append("_id", customer.getVehicleNo())
            .append("vehicletype", customer.getVehicleType())
            .append("dateOfDelivery", customer.getDateOfDelivery()).append("engineNo", customer.getEngineNo())
            .append("chassisNo", customer.getChassisNo()).append("freeServiceNo", customer.getFreeServiceNo())
            .append("payment", customer.getPayment()).append("serviceNo", customer.getServiceNo())
            .append("lastKm", customer.getLastKm()).append("phoneNo", customer.getPhoneNo());

    coll.insert(doc);
    System.out.println("successfully");

}

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   w  ww .ja v a  2  s.c  o 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);
}