List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:com.techngage.smartbin.RouteDAO.java
License:Apache License
public RouteDAO(final MongoDatabase smartbinDatabase) { routeCollection = smartbinDatabase.getCollection("route"); }
From source file:com.techngage.smartbin.SessionDAO.java
License:Apache License
public SessionDAO(final MongoDatabase smartbinDatabase) { sessionsCollection = smartbinDatabase.getCollection("sessions"); }
From source file:com.techngage.smartbin.TruckDAO.java
License:Apache License
public TruckDAO(final MongoDatabase smartbinDatabase) { truckCollection = smartbinDatabase.getCollection("truck"); }
From source file:com.techngage.smartbin.UserDAO.java
License:Apache License
public UserDAO(final MongoDatabase smartbinDatabase) { usersCollection = smartbinDatabase.getCollection("users"); }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java
License:Open Source License
/** * Inserts a new document in the given raw collection within the given database (row-like mode). * @param dbName/*w ww . j ava 2s .c o m*/ * @param collectionName * @param recvTimeTs * @param recvTime * @param entityId * @param entityType * @param attrName * @param attrType * @param attrValue * @param attrMd * @throws Exception */ public void insertContextDataRaw(String dbName, String collectionName, long recvTimeTs, String recvTime, String entityId, String entityType, String attrName, String attrType, String attrValue, String attrMd) throws Exception { MongoDatabase db = getDatabase(dbName); MongoCollection collection = db.getCollection(collectionName); Document doc = new Document("recvTime", new Date(recvTimeTs * 1000)); switch (dataModel) { case COLLECTIONPERSERVICEPATH: doc.append("entityId", entityId).append("entityType", entityType).append("attrName", attrName) .append("attrType", attrType).append("attrValue", attrValue); break; case COLLECTIONPERENTITY: doc.append("attrName", attrName).append("attrType", attrType).append("attrValue", attrValue); break; case COLLECTIONPERATTRIBUTE: doc.append("attrType", attrType).append("attrValue", attrValue); break; default: // this will never be reached } // switch LOGGER.debug("Inserting data=" + doc.toString() + " within collection=" + collectionName); collection.insertOne(doc); }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java
License:Open Source License
/** * Inserts a new document with given resolution in the given aggregated collection within the given database * (row-like mode)./*from w ww .j a va 2 s . c om*/ * @param dbName * @param collectionName * @param recvTimeTs * @param entityId * @param entityType * @param attrName * @param attrType * @param attrValue * @param resolution */ private void insertContextDataAggregatedForResoultion(String dbName, String collectionName, GregorianCalendar calendar, String entityId, String entityType, String attrName, String attrType, double attrValue, Resolution resolution) { // get database and collection MongoDatabase db = getDatabase(dbName); MongoCollection collection = db.getCollection(collectionName); // build the query BasicDBObject query = buildQueryForInsertAggregated(calendar, entityId, entityType, attrName, resolution); // prepopulate if needed BasicDBObject insert = buildInsertForPrepopulate(attrType, resolution); UpdateResult res = collection.updateOne(query, insert, new UpdateOptions().upsert(true)); if (res.getMatchedCount() == 0) { LOGGER.debug("Prepopulating data, database=" + dbName + ", collection=" + collectionName + ", query=" + query.toString() + ", insert=" + insert.toString()); } // if // do the update BasicDBObject update = buildUpdateForUpdate(attrType, attrValue); LOGGER.debug("Updating data, database=" + dbName + ", collection=" + collectionName + ", query=" + query.toString() + ", update=" + update.toString()); collection.updateOne(query, update); }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackend.java
License:Open Source License
/** * Stores in per-service/database "collection_names" collection the matching between a hash and the fields used to * build it.//from w w w .ja v a2s.c om * FIXME: destination is under study * @param dbName * @param hash * @param isAggregated * @param fiwareService * @param fiwareServicePath * @param entityId * @param entityType * @param attrName * @param destination * @throws java.lang.Exception */ public void storeCollectionHash(String dbName, String hash, boolean isAggregated, String fiwareService, String fiwareServicePath, String entityId, String entityType, String attrName, String destination) throws Exception { // get the database and the collection; the collection is created if not existing MongoDatabase db = getDatabase(dbName); MongoCollection collection; try { LOGGER.debug("Creating Mongo collection=collection_names at database=" + dbName); db.createCollection("collection_names"); } catch (Exception e) { if (e.getMessage().contains("collection already exists")) { LOGGER.debug("Collection already exists, nothing to create"); } else { throw e; } // if else } finally { collection = db.getCollection("collection_names"); } // try catch finally // Two updates operations are needed since MongoDB currently does not support the possibility to address the // same field in a $set operation as a $setOnInsert operation. More details: // http://stackoverflow.com/questions/23992723/ \ // findandmodify-fails-with-error-cannot-update-field1-and-field1-at-the-same // build the query BasicDBObject query = new BasicDBObject().append("_id", hash + (isAggregated ? ".aggr" : "")); // do the first update BasicDBObject update = buildUpdateForCollectionHash("$setOnInsert", isAggregated, fiwareService, fiwareServicePath, entityId, entityType, attrName, destination); LOGGER.debug("Updating data, database=" + dbName + ", collection=collection_names, query=" + query.toString() + ", update=" + update.toString()); UpdateResult res = collection.updateOne(query, update, new UpdateOptions().upsert(true)); /* TECHDEBT: https://github.com/telefonicaid/fiware-cygnus/issues/428 https://github.com/telefonicaid/fiware-cygnus/issues/429 if (res.getMatchedCount() == 0) { LOGGER.error("There was an error when storing the collecion hash, database=" + dbName + ", collection=collection_names, query=" + query.toString() + ", update=" + update.toString()); return; } // if // do the second update update = buildUpdateForCollectionHash("$set", isAggregated, fiwareService, fiwareServicePath, entityId, entityType, attrName, destination); LOGGER.debug("Updating data, database=" + dbName + ", collection=collection_names, query=" + query.toString() + ", update=" + update.toString()); res = collection.updateOne(query, update); if (res.getMatchedCount() == 0) { LOGGER.error("There was an error when storing the collecion hash, database=" + dbName + ", collection=collection_names, query=" + query.toString() + ", update=" + update.toString()); } // if */ }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackendImpl.java
License:Open Source License
/** * Creates a collection for STH, given its name, if not exists in the given database. Time-based limits are set, * if possible.// w ww .ja v a 2 s . c o m * @param dbName * @param collectionName * @param dataExpiration * @throws Exception */ @Override public void createCollection(String dbName, String collectionName, long dataExpiration) throws Exception { LOGGER.debug("Creating Mongo collection=" + collectionName + " at database=" + dbName); MongoDatabase db = getDatabase(dbName); // create the collection try { db.createCollection(collectionName); } catch (Exception e) { if (e.getMessage().contains("collection already exists")) { LOGGER.debug("Collection already exists, nothing to create"); } else { throw e; } // if else } // try catch // ensure the _id.origin index, if possible try { if (dataExpiration != 0) { BasicDBObject keys = new BasicDBObject().append("_id.origin", 1); IndexOptions options = new IndexOptions().expireAfter(dataExpiration, TimeUnit.SECONDS); db.getCollection(collectionName).createIndex(keys, options); } // if } catch (Exception e) { throw e; } // try catch }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackendImpl.java
License:Open Source License
/** * Creates a collection for plain MongoDB, given its name, if not exists in the given database. Size-based limits * are set, if possible. Time-based limits are also set, if possible. * @param dbName//from www . j a v a 2 s . c o m * @param collectionName * @param collectionsSize * @param maxDocuments * @throws Exception */ @Override public void createCollection(String dbName, String collectionName, long collectionsSize, long maxDocuments, long dataExpiration) throws Exception { MongoDatabase db = getDatabase(dbName); // create the collection, with size-based limits if possible try { if (collectionsSize != 0 && maxDocuments != 0) { CreateCollectionOptions options = new CreateCollectionOptions().capped(true) .sizeInBytes(collectionsSize).maxDocuments(maxDocuments); LOGGER.debug("Creating Mongo collection=" + collectionName + " at database=" + dbName + " with " + "collections_size=" + collectionsSize + " and max_documents=" + maxDocuments + " options"); db.createCollection(collectionName, options); } else { LOGGER.debug("Creating Mongo collection=" + collectionName + " at database=" + dbName); db.createCollection(collectionName); } // if else } catch (Exception e) { if (e.getMessage().contains("collection already exists")) { LOGGER.debug("Collection already exists, nothing to create"); } else { throw e; } // if else } // try catch // ensure the recvTime index, if possible try { if (dataExpiration != 0) { BasicDBObject keys = new BasicDBObject().append("recvTime", 1); IndexOptions options = new IndexOptions().expireAfter(dataExpiration, TimeUnit.SECONDS); db.getCollection(collectionName).createIndex(keys, options); } // if } catch (Exception e) { throw e; } // try catch }
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackendImpl.java
License:Open Source License
/** * Inserts a new document in the given raw collection within the given database (row-like mode). * @param dbName/*from w ww . j av a 2 s .c om*/ * @param collectionName * @param aggregation * @throws Exception */ @Override public void insertContextDataRaw(String dbName, String collectionName, ArrayList<Document> aggregation) throws Exception { MongoDatabase db = getDatabase(dbName); MongoCollection collection = db.getCollection(collectionName); collection.insertMany(aggregation); }