Example usage for com.mongodb.client.model Updates push

List of usage examples for com.mongodb.client.model Updates push

Introduction

In this page you can find the example usage for com.mongodb.client.model Updates push.

Prototype

public static <TItem> Bson push(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates an update that adds the given value to the array value of the field with the given name.

Usage

From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java

License:Apache License

@Override
public void addQueryFilter(String userId, QueryFilter queryFilter) throws CatalogDBException {
    // Check if there exists a filter for that user with the same id
    Bson checkFilterExists = Filters.and(Filters.eq(QueryParams.ID.key(), userId),
            Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId()));
    QueryResult<Long> count = userCollection.count(checkFilterExists);
    if (count.getResult().get(0) != 0) {
        throw new CatalogDBException(
                "There already exists a filter with name " + queryFilter.getId() + " for user " + userId);
    }/*from  w w w .  j av a  2  s  .  co m*/

    // Insert the filter
    Bson query = Filters.and(Filters.eq(QueryParams.ID.key(), userId),
            Filters.ne(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId()));

    Bson filterDocument = getMongoDBDocument(queryFilter, "Filter");
    Bson update = Updates.push(QueryParams.CONFIG_OPENCGA_FILTERS.key(), filterDocument);

    QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null);

    if (queryResult.first().getModifiedCount() != 1) {
        if (queryResult.first().getModifiedCount() == 0) {
            throw new CatalogDBException(
                    "User: There was an error when trying to store the filter. It could not be stored.");
        } else {
            // This error should NEVER be raised.
            throw new CatalogDBException(
                    "User: There was a critical error when storing the filter. Is has been inserted "
                            + queryResult.first().getModifiedCount() + " times.");
        }
    }
}