List of usage examples for com.mongodb.client.model Updates combine
public static Bson combine(final List<? extends Bson> updates)
From source file:org.nuxeo.directory.mongodb.MongoDBSession.java
License:Apache License
@Override public void updateEntry(DocumentModel docModel) throws DirectoryException { checkPermission(SecurityConstants.WRITE); Map<String, Object> fieldMap = new HashMap<>(); List<String> referenceFieldList = new LinkedList<>(); for (String fieldName : schemaFieldMap.keySet()) { Property prop = docModel.getPropertyObject(schemaName, fieldName); if (fieldName.equals(getPasswordField()) && StringUtils.isEmpty((String) prop.getValue())) { continue; }//w w w . ja v a2 s.com if (prop != null && prop.isDirty()) { Serializable value = prop.getValue(); if (fieldName.equals(getPasswordField())) { value = PasswordHelper.hashPassword((String) value, passwordHashAlgorithm); } fieldMap.put(prop.getName(), value); } if (getDirectory().isReference(fieldName)) { referenceFieldList.add(fieldName); } } String id = docModel.getId(); Document bson = MongoDBSerializationHelper.fieldMapToBson(getIdField(), id); List<Bson> updates = fieldMap.entrySet().stream().map(e -> Updates.set(e.getKey(), e.getValue())) .collect(Collectors.toList()); try { UpdateResult result = getCollection().updateOne(bson, Updates.combine(updates)); // Throw an error if no document matched the update if (!result.wasAcknowledged()) { throw new DirectoryException( "Error while updating the entry, the request has not been acknowledged by the server"); } if (result.getMatchedCount() == 0) { throw new DirectoryException( String.format("Error while updating the entry, no document was found with the id %s", id)); } } catch (MongoWriteException e) { throw new DirectoryException(e); } // update reference fields for (String referenceFieldName : referenceFieldList) { List<Reference> references = directory.getReferences(referenceFieldName); if (references.size() > 1) { // not supported log.warn("Directory " + getDirectory().getName() + " cannot update field " + referenceFieldName + " for entry " + docModel.getId() + ": this field is associated with more than one reference"); } else { Reference reference = references.get(0); @SuppressWarnings("unchecked") List<String> targetIds = (List<String>) docModel.getProperty(schemaName, referenceFieldName); if (reference instanceof MongoDBReference) { MongoDBReference mongoReference = (MongoDBReference) reference; mongoReference.setTargetIdsForSource(docModel.getId(), targetIds, this); } else { reference.setTargetIdsForSource(docModel.getId(), targetIds); } } } getDirectory().invalidateCaches(); }
From source file:org.opencb.opencga.storage.mongodb.metadata.MongoDBStudyConfigurationManager.java
License:Apache License
@Override public QueryResult internalUpdateStudyConfiguration(StudyConfiguration studyConfiguration, QueryOptions options) {/* w w w .ja v a 2s . co m*/ Document studyMongo = new DocumentToStudyConfigurationConverter().convertToStorageType(studyConfiguration); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" Document query = new Document("_id", studyConfiguration.getStudyId()); List<Bson> updates = new ArrayList<>(studyMongo.size()); studyMongo.forEach((s, o) -> updates.add(new Document("$set", new Document(s, o)))); QueryResult<UpdateResult> queryResult = collection.update(query, Updates.combine(updates), new QueryOptions(UPSERT, true)); // studyConfigurationMap.put(studyConfiguration.getStudyId(), studyConfiguration); return queryResult; }