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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

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

License:Apache License

@Override
public QueryResult resetPassword(String userId, String email, String newCryptPass) throws CatalogDBException {
    long startTime = startQuery();

    //        BasicDBObject query = new BasicDBObject("id", userId);
    //        query.put("email", email);
    Query query = new Query(QueryParams.ID.key(), userId);
    query.append(QueryParams.EMAIL.key(), email);
    Bson bson = parseQuery(query);//  w  w  w.j  av a 2s. c  om

    //        BasicDBObject fields = new BasicDBObject("password", newCryptPass);
    //        BasicDBObject action = new BasicDBObject("$set", fields);
    Bson set = Updates.set("password", new Document("password", newCryptPass));

    //        QueryResult<WriteResult> update = userCollection.update(query, action, null);
    QueryResult<UpdateResult> update = userCollection.update(bson, set, null);
    if (update.getResult().get(0).getModifiedCount() == 0) { //0 query matches.
        throw new CatalogDBException("Bad user or email");
    }
    return endQuery("Reset Password", startTime, Arrays.asList("Password successfully changed"));
}

From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult updateCustomAnnotations(Query query, String name, AdditionalAttribute attribute,
        QueryOptions options) {/*from  w  w w. jav a 2 s . c  o  m*/
    Document queryDocument = parseQuery(query);
    Document updateDocument = DocumentToVariantAnnotationConverter.convertToStorageType(attribute);
    return variantsCollection.update(queryDocument,
            Updates.set(DocumentToVariantConverter.CUSTOM_ANNOTATION_FIELD + "." + name, updateDocument),
            new QueryOptions(MULTI, true));
}

From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantSourceMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult updateSourceStats(VariantSourceStats variantSourceStats,
        StudyConfiguration studyConfiguration, QueryOptions queryOptions) {
    MongoDBCollection coll = db.getCollection(collectionName);

    VariantSource source = new VariantSource(new VariantFileMetadata());
    source.setStats(variantSourceStats.getFileStats());
    Document globalStats = variantSourceConverter.convertToStorageType(source).get("stats", Document.class);

    Bson query = parseQuery(new Query(VariantSourceQueryParam.STUDY_ID.key(), variantSourceStats.getStudyId())
            .append(VariantSourceQueryParam.FILE_ID.key(), variantSourceStats.getFileId()));
    Bson update = Updates.set("stats", globalStats);

    return coll.update(query, update, null);
}

From source file:org.trade.core.persistence.local.mongo.MongoPersistence.java

License:Apache License

@Override
public void storeBinaryData(byte[] data, String collectionName, String identifier) throws Exception {
    MongoClient client = new MongoClient(new MongoClientURI(this.mongoUrl));
    MongoDatabase db = client.getDatabase(this.dbName);

    MongoCollection<Document> collection = db.getCollection(collectionName);
    Document doc = collection.find(Filters.eq(IDENTIFIER_FIELD, identifier)).limit(1).first();

    if (data == null) {
        // We assume that if the value is set to null, we should delete also the corresponding database entry
        if (doc != null) {
            collection.deleteOne(Filters.eq(IDENTIFIER_FIELD, identifier));
        }//from w  ww .j a  v  a  2  s . c  o m
    } else {
        // Check if the document already exists and update it
        if (doc != null) {
            collection.updateOne(Filters.eq(IDENTIFIER_FIELD, identifier),
                    Updates.combine(Updates.set(DATA_FIELD, data), Updates.currentDate("lastModified")));
        } else {
            Document document = new Document(IDENTIFIER_FIELD, identifier).append(DATA_FIELD, data)
                    .append("lastModified", new Date());
            collection.insertOne(document);
        }
    }

    client.close();
}

From source file:uk.ac.ebi.eva.dbmigration.mongodb.ExtractAnnotationFromVariant.java

License:Apache License

private UpdateOneModel<Document> buildUpdateDocument(Document variantDocument) {
    Document annotationSubdocument = (Document) variantDocument.get(ANNOT_FIELD);
    Assert.notNull(annotationSubdocument, "Logic error");

    Set<Integer> soSet = computeSoSet(annotationSubdocument);
    Set<String> xrefSet = computeXrefSet(annotationSubdocument);
    List<Double> sift = computeMinAndMaxScore(annotationSubdocument, SIFT_FIELD);
    List<Double> polyphen = computeMinAndMaxScore(annotationSubdocument, POLYPHEN_FIELD);

    Document newAnnotationSubdocument = new Document()
            .append(VEP_VERSION_FIELD, databaseParameters.getVepVersion())
            .append(CACHE_VERSION_FIELD, databaseParameters.getVepCacheVersion());

    if (!soSet.isEmpty()) {
        newAnnotationSubdocument.append(SO_FIELD, soSet);
    }//from   w w  w. j  a  v a2  s . c o m
    if (!xrefSet.isEmpty()) {
        newAnnotationSubdocument.append(XREFS_FIELD, xrefSet);
    }
    if (!sift.isEmpty()) {
        newAnnotationSubdocument.append(SIFT_FIELD, sift);
    }
    if (!polyphen.isEmpty()) {
        newAnnotationSubdocument.append(POLYPHEN_FIELD, polyphen);
    }

    List<Document> newAnnotationArray = Collections.singletonList(newAnnotationSubdocument);

    Document query = new Document(ID_FIELD, variantDocument.get(ID_FIELD));
    Bson update = Updates.set(ANNOT_FIELD, newAnnotationArray);
    return new UpdateOneModel<>(query, update);
}