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

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

Introduction

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

Prototype

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

Source Link

Document

Creates an update that removes all instances of the given value from the array value of the field with the given name.

Usage

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java

License:Open Source License

@Override
public void deleteUserGroups(final String organizationId, final String userId) {
    Bson pullUser = Updates.pull(UserCodec.ALL_GROUPS, Filters.eq(UserCodec.ORGANIZATION_ID, organizationId));
    try {//from ww w .java2  s  .  c  o  m
        UpdateResult result = databaseCollection().updateOne(idFilter(userId), pullUser);
        if (result.getModifiedCount() != 1) {
            throw new StorageException("User '" + userId + "' has not been deleted.");
        }
    } catch (MongoException ex) {
        throw new StorageException("Cannot remove organization " + organizationId + "from user " + userId, ex);
    }
}

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java

License:Open Source License

@Override
public void deleteUsersGroups(final String organizationId) {
    Bson pullUser = Updates.pull(UserCodec.ALL_GROUPS, Filters.eq(UserCodec.ORGANIZATION_ID, organizationId));
    try {//  ww w  .  j  av a2  s.c  o  m
        databaseCollection().updateMany(new BsonDocument(), pullUser);
    } catch (MongoException ex) {
        throw new StorageException("Cannot remove organization " + organizationId + " from users", ex);
    }
}

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java

License:Open Source License

@Override
public void deleteGroupFromUsers(final String organizationId, final String group) {
    String key = MongoUtils.concatParams(UserCodec.ALL_GROUPS, "$[" + ELEMENT_NAME + "]", UserCodec.GROUPS);
    Bson pullGroups = Updates.pull(key, group);
    UpdateOptions options = new UpdateOptions().arrayFilters(arrayFilters(organizationId));

    databaseCollection().updateMany(new BsonDocument(), pullGroups, options);
}

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

License:Apache License

@Override
public QueryResult setProjectAcl(long projectId, AclEntry newAcl) throws CatalogDBException {
    long startTime = startQuery();
    String userId = newAcl.getUserId();
    if (!dbAdaptorFactory.getCatalogUserDBAdaptor().userExists(userId)) {
        throw new CatalogDBException("Can not set ACL to non-existent user: " + userId);
    }//w  ww. j ava2  s. co  m

    Document newAclObject = getMongoDBDocument(newAcl, "ACL");
    //DBObject newAclObject = getDbObject(newAcl, "ACL");

    List<AclEntry> projectAcls = getProjectAcl(projectId, userId).getResult();
    Bson query = new Document("projects.id", projectId);
    Bson push = Updates.push("projects.$.acl", newAclObject);
    if (!projectAcls.isEmpty()) { // ensure that there is no acl for that user in that project. pull
        Bson pull = Updates.pull("projects.$.acl", Filters.eq("userId", userId));
        userCollection.update(query, pull, null);
    }
    /*
    DBObject query = new BasicDBObject("projects.id", projectId);
    BasicDBObject push = new BasicDBObject("$push", new BasicDBObject("projects.$.acl", newAclObject));
    if (!projectAcls.isEmpty()) {  // ensure that there is no acl for that user in that project. pull
    DBObject pull = new BasicDBObject("$pull", new BasicDBObject("projects.$.acl", new BasicDBObject("userId", userId)));
    userCollection.update(query, pull, null);
    }
    */
    //Put study
    QueryResult pushResult = userCollection.update(query, push, null);
    return endQuery("Set project acl", startTime, pushResult);
}

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

License:Apache License

@Override
public QueryResult<VariableSet> renameFieldVariableSet(long variableSetId, String oldName, String newName)
        throws CatalogDBException {
    long startTime = startQuery();

    checkVariableSetExists(variableSetId);
    checkVariableInVariableSet(variableSetId, oldName);
    checkVariableNotInVariableSet(variableSetId, newName);

    // The field can be changed if we arrive to this point.
    // 1. we obtain the variable
    Variable variable = getVariable(variableSetId, oldName);

    // 2. we take it out from the array.
    Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId);
    Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(),
            Filters.eq("name", oldName));
    QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null);

    if (queryResult.first().getModifiedCount() == 0) {
        throw new CatalogDBException(
                "VariableSet {id: " + variableSetId + "} - Could not rename the field " + oldName);
    }/*from  ww w  .ja v a2 s.co  m*/
    if (queryResult.first().getModifiedCount() > 1) {
        throw new CatalogDBException("VariableSet {id: " + variableSetId
                + "} - An unexpected error happened when extracting the "
                + "variable from the variableSet to do the rename. Please, report this error to the OpenCGA developers.");
    }

    // 3. we change the name in the variable object and push it again in the array.
    variable.setName(newName);
    update = Updates.push(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(),
            getMongoDBDocument(variable, "Variable"));
    queryResult = studyCollection.update(bsonQuery, update, null);

    if (queryResult.first().getModifiedCount() != 1) {
        throw new CatalogDBException("VariableSet {id: " + variableSetId
                + "} - A critical error happened when trying to rename one "
                + "of the variables of the variableSet object. Please, report this error to the OpenCGA developers.");
    }

    // 4. Change the field id in the annotations
    dbAdaptorFactory.getCatalogSampleDBAdaptor().renameAnnotationField(variableSetId, oldName, newName);
    dbAdaptorFactory.getCatalogCohortDBAdaptor().renameAnnotationField(variableSetId, oldName, newName);

    return endQuery("Rename field in variableSet", startTime, getVariableSet(variableSetId, null));
}

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

License:Apache License

@Override
public QueryResult<VariableSet> removeFieldFromVariableSet(long variableSetId, String name)
        throws CatalogDBException {
    long startTime = startQuery();

    try {/*from ww w . j  a v a 2s.c o  m*/
        checkVariableInVariableSet(variableSetId, name);
    } catch (CatalogDBException e) {
        checkVariableSetExists(variableSetId);
        throw e;
    }
    Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId);
    Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(),
            Filters.eq("name", name));
    QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null);
    if (queryResult.first().getModifiedCount() != 1) {
        throw new CatalogDBException("Remove field from Variable Set. Could not remove the field " + name
                + " from the variableSet id " + variableSetId);
    }

    // Remove all the annotations from that field
    dbAdaptorFactory.getCatalogSampleDBAdaptor().removeAnnotationField(variableSetId, name);
    dbAdaptorFactory.getCatalogCohortDBAdaptor().removeAnnotationField(variableSetId, name);

    return endQuery("Remove field from Variable Set", startTime, getVariableSet(variableSetId, null));
}

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

License:Apache License

@Override
public QueryResult<VariableSet> deleteVariableSet(long variableSetId, QueryOptions queryOptions)
        throws CatalogDBException {
    long startTime = startQuery();

    checkVariableSetInUse(variableSetId);
    long studyId = getStudyIdByVariableSetId(variableSetId);
    QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, queryOptions);
    Bson query = Filters.eq(PRIVATE_ID, studyId);
    Bson operation = Updates.pull("variableSets", Filters.eq("id", variableSetId));
    QueryResult<UpdateResult> update = studyCollection.update(query, operation, null);

    if (update.first().getModifiedCount() == 0) {
        throw CatalogDBException.idNotFound("VariableSet", variableSetId);
    }/*from   ww w .j a  va  2s  .co  m*/
    return endQuery("Delete VariableSet", startTime, variableSet);
}

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

License:Apache License

@Override
public QueryResult<AnnotationSet> deleteAnnotation(long individualId, String annotationId)
        throws CatalogDBException {

    long startTime = startQuery();

    Individual individual = get(individualId,
            new QueryOptions("include", "projects.studies.individuals.annotationSets")).first();
    AnnotationSet annotationSet = null;/*from  w  w  w  . j ava  2 s.  c om*/
    for (AnnotationSet as : individual.getAnnotationSets()) {
        if (as.getName().equals(annotationId)) {
            annotationSet = as;
            break;
        }
    }

    if (annotationSet == null) {
        throw CatalogDBException.idNotFound("AnnotationSet", annotationId);
    }

    //        DBObject query = new BasicDBObject(PRIVATE_ID, individualId);
    //        DBObject update = new BasicDBObject("$pull", new BasicDBObject("annotationSets", new BasicDBObject("id", annotationId)));
    //        QueryResult<WriteResult> resultQueryResult = individualCollection.update(query, update, null);
    //        if (resultQueryResult.first().getN() < 1) {
    //            throw CatalogDBException.idNotFound("AnnotationSet", annotationId);
    //        }

    Bson eq = Filters.eq(PRIVATE_ID, individualId);
    Bson pull = Updates.pull("annotationSets", new Document("name", annotationId));
    QueryResult<UpdateResult> update = individualCollection.update(eq, pull, null);
    if (update.first().getModifiedCount() < 1) {
        throw CatalogDBException.idNotFound("AnnotationSet", annotationId);
    }

    return endQuery("Delete annotation", startTime, Collections.singletonList(annotationSet));
}

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

License:Apache License

@Override
@Deprecated/*from  www.  j  a  v a 2  s .com*/
public QueryResult<AnnotationSet> deleteAnnotation(long sampleId, String annotationId)
        throws CatalogDBException {

    long startTime = startQuery();

    Sample sample = get(sampleId, new QueryOptions("include", "projects.studies.samples.annotationSets"))
            .first();
    AnnotationSet annotationSet = null;
    for (AnnotationSet as : sample.getAnnotationSets()) {
        if (as.getName().equals(annotationId)) {
            annotationSet = as;
            break;
        }
    }

    if (annotationSet == null) {
        throw CatalogDBException.idNotFound("AnnotationSet", annotationId);
    }

    /*
    DBObject query = new BasicDBObject(PRIVATE_ID, sampleId);
    DBObject update = new BasicDBObject("$pull", new BasicDBObject("annotationSets", new BasicDBObject("id", annotationId)));
    */
    Bson query = new Document(PRIVATE_ID, sampleId);
    Bson update = Updates.pull("annotationSets", new Document("name", annotationId));
    QueryResult<UpdateResult> resultQueryResult = sampleCollection.update(query, update, null);
    if (resultQueryResult.first().getModifiedCount() < 1) {
        throw CatalogDBException.idNotFound("AnnotationSet", annotationId);
    }

    return endQuery("Delete annotation", startTime, Collections.singletonList(annotationSet));
}

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

License:Apache License

@Override
public QueryResult<Long> deleteQueryFilter(String userId, String filterId) throws CatalogDBException {
    long startTime = startQuery();
    // Delete the filter from the database
    Bson query = Filters.and(Filters.eq(QueryParams.ID.key(), userId),
            Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", filterId));

    Bson update = Updates.pull(QueryParams.CONFIG_OPENCGA_FILTERS.key(), Filters.eq("id", filterId));
    QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null);
    return endQuery("Delete query filter", startTime,
            Collections.singletonList(queryResult.first().getModifiedCount()));
}