@Override public QueryResult updateProjectMetadata(ProjectMetadata projectMetadata, boolean updateCounters) { Document mongo = new GenericDocumentComplexConverter<>(ProjectMetadata.class).convertToStorageType(projectMetadata); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" List<Bson> updates = new ArrayList<>(mongo.size()); mongo.forEach((s, o) -> { // Do not update counters if (updateCounters || !s.equals(COUNTERS_FIELD)) { updates.add(new Document("$set", new Document(s, o))); } }); return collection.update(QUERY, Updates.combine(updates), new QueryOptions(UPSERT, true)); }
public MongoDBProjectMetadataDBAdaptor(MongoDataStore db, String collectionName) { converter = new GenericDocumentComplexConverter<>(ProjectMetadata.class); this.collection = db.getCollection(collectionName) .withReadPreference(ReadPreference.primary()) .withWriteConcern(WriteConcern.ACKNOWLEDGED); mongoLock = new MongoLock(collection, "_lock"); }