Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:org.ff4j.mongo.store.EventRepositoryMongo.java

License:Apache License

/**
 * Parameterized constructor with collection.
 * //  w w w.j  a v  a  2 s  . co m
 * @param collection
 *            the collection to set
 */
public EventRepositoryMongo(MongoDatabase db, String collectionName) {
    this.eventsCollection = db.getCollection(collectionName);
}

From source file:org.ff4j.mongo.store.FeatureStoreMongo.java

License:Apache License

/**
 * Parameterized constructor with collection.
 * //  www .  j a  v a 2s .c o m
 * @param collection
 *            the collection to set
 */
public FeatureStoreMongo(MongoDatabase db, String collectionName) {
    this.dbName = db.getName();
    this.featuresCollection = db.getCollection(collectionName);
}

From source file:org.ff4j.mongo.store.PropertyStoreMongo.java

License:Apache License

/**
 * Parameterized constructor with collection.
 * /* w  ww. j  a  va2s  .com*/
 * @param collection
 *            the collection to set
 */
public PropertyStoreMongo(MongoDatabase db, String collectionName) {
    this.propertiesCollection = db.getCollection(collectionName);
    this.dbName = db.getName();
}

From source file:org.flywaydb.core.internal.command.MongoClean.java

License:Apache License

@Override
public void clean() throws FlywayException {
    if (cleanDisabled) {
        throw new FlywayException(
                "Unable to execute clean as it has been disabled with the \"flyway.cleanDisabled\" property.");
    }//  w  w  w  .  j av a2  s  . co  m

    for (final MongoFlywayCallback callback : callbacks) {
        callback.beforeClean(client);
    }

    for (String dbName : client.listDatabaseNames()) {
        MongoDatabase db = client.getDatabase(dbName);
        for (String collectionName : db.listCollectionNames()) {
            db.getCollection(collectionName).drop();
        }
    }
    LOG.info("Successfully cleaned Mongo.");

    for (final MongoFlywayCallback callback : callbacks) {
        callback.afterClean(client);
    }
}

From source file:org.gennai.gungnir.topology.processor.MongoFetchProcessor.java

License:Apache License

@Override
public void open(GungnirConfig config, GungnirContext context) throws ProcessorException {
    dbName = context.replaceVariable(dbName);
    collectionName = context.replaceVariable(collectionName);
    query = parseQueryString(queryString);
    fetchFields = new Document();
    for (String fieldName : fetchFieldNames) {
        fetchFields.append(fieldName, 1);
    }// w w  w.j a v a  2 s.c om
    if (sortString != null) {
        sort = parseSortString(sortString);
    }
    if (expire != null) {
        expireSecs = expire.toSeconds();
    }

    List<String> servers = config.getList(FETCH_SERVERS);
    List<ServerAddress> addresses = Lists.newArrayListWithCapacity(servers.size());
    for (String server : servers) {
        addresses.add(new ServerAddress(server));
    }
    mongoClient = new MongoClient(addresses);
    MongoDatabase db = mongoClient.getDatabase(dbName);
    collection = db.getCollection(collectionName);

    if (expireSecs > 0) {
        cache = CacheBuilder.newBuilder().maximumSize(config.getInteger(CACHE_SIZE))
                .expireAfterWrite(expireSecs, TimeUnit.SECONDS).build();
    }

    LOG.info("MongoFetchProcessor opened({})", this);
}

From source file:org.gennai.gungnir.topology.processor.MongoPersistProcessor.java

License:Apache License

@Override
public void open(GungnirConfig config, GungnirContext context, OperatorContext operatorContext,
        Map<String, List<String>> outputFieldNames) throws ProcessorException {
    dbName = context.replaceVariable(dbName);
    collectionName = context.replaceVariable(collectionName);
    this.outputFieldNames = outputFieldNames;

    if (keyFieldNames != null) {
        keyFieldsIndexes = Maps.newHashMap();
        for (Map.Entry<String, List<String>> entry : outputFieldNames.entrySet()) {
            int[] index = new int[keyFieldNames.length];
            Arrays.fill(index, -1);
            for (int i = 0; i < keyFieldNames.length; i++) {
                for (int j = 0; j < entry.getValue().size(); j++) {
                    if (entry.getValue().get(j).equals(keyFieldNames[i])) {
                        index[i] = j;/*from w  w w. j  a  va 2 s .  co m*/
                        break;
                    }
                }
                if (index[i] < 0) {
                    throw new ProcessorException("Can't found key field '" + keyFieldNames[i] + "'");
                }
            }
            keyFieldsIndexes.put(entry.getKey(), index);
        }
    }

    List<String> servers = config.getList(MONGO_PERSIST_SERVERS);
    List<ServerAddress> addresses = Lists.newArrayListWithCapacity(servers.size());
    for (String server : servers) {
        addresses.add(new ServerAddress(server));
    }
    mongoClient = new MongoClient(addresses);
    MongoDatabase db = mongoClient.getDatabase(dbName);
    collection = db.getCollection(collectionName);

    if (autoIndexing && keyFieldNames != null) {
        Document doc = new Document();
        for (String keyFieldName : keyFieldNames) {
            doc.append(keyFieldName, 1);
        }

        collection.createIndex(doc, new IndexOptions().unique(true));
    }

    LOG.info("MongoPersistProcessor opened({})", this);
}

From source file:org.httprpc.examples.mongodb.RestaurantService.java

License:Apache License

/**
 * Retrieves a list of restaurants in a given zip code.
 *
 * @param zipCode/*from w  w  w .  j a  v a 2s.  c  o m*/
 * The zip code to search for.
 *
 * @return
 * A list of restaurants in the given zip code.
 */
@RPC(method = "GET")
@Template(name = "restaurants.html", contentType = "text/html")
public IteratorAdapter getRestaurants(String zipCode) {
    MongoDatabase db = MongoClientManager.getMongoClient().getDatabase("test");
    FindIterable<Document> iterable = db.getCollection("restaurants")
            .find(new Document("address.zipcode", zipCode));

    return new IteratorAdapter(iterable.iterator());
}

From source file:org.iu.sead.cloud.ROSearch.java

License:Apache License

public ROSearch() {
    MongoDatabase db = MongoDB.getServicesDB();
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    control.setNoCache(true);/*from   w w  w. ja v a  2 s. c  o  m*/
    pdtResource = Client.create().resource(Constants.pdtURL);
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public void persist(JaqpotEntity entity) {
    MongoDatabase db = mongoClient.getDatabase(database);
    String entityJSON = serializer.write(entity);
    MongoCollection collection = db.getCollection(collectionNames.get(entity.getClass()));
    Document entityBSON = Document.parse(entityJSON);
    try {/*from   w w  w.j a  v  a 2 s .  co  m*/
        collection.insertOne(entityBSON);
    } catch (final MongoWriteException ex) {
        String errorMessage = "Entity with ID " + entity.getId()
                + " is already registered and will not be overwritten!";
        LOG.log(Level.FINE, errorMessage, ex);
        throw ex;
    }
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> T merge(T entity) {
    MongoDatabase db = mongoClient.getDatabase(database);
    Class<T> entityClass = (Class<T>) entity.getClass();
    String entityJSON = serializer.write(entity);
    Document entityBSON = Document.parse(entityJSON);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entity.getClass()));
    Document oldEntity = collection.findOneAndReplace(new Document("_id", entity.getId()), entityBSON);
    return serializer.parse(JSON.serialize(oldEntity), entityClass);
}