@Override public void addSamplesToFile(long fileId, List<Sample> samples) throws CatalogDBException { if (samples == null || samples.size() == 0) { return; } List<Document> sampleList = fileConverter.convertSamples(samples); Bson update = Updates.addEachToSet(QueryParams.SAMPLES.key(), sampleList); fileCollection.update(Filters.eq(PRIVATE_UID, fileId), update, QueryOptions.empty()); }
@Test public void testUpdateAddToSetEach() throws Exception { collection.insertOne(json("_id: 1")); collection.updateOne(json("_id: 1"), addEachToSet("a", Arrays.asList(6, 5, 4))); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: [6, 5, 4]")); collection.updateOne(json("_id: 1"), addEachToSet("a", Arrays.asList(3, 2, 1))); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: [6, 5, 4, 3, 2, 1]")); collection.updateOne(json("_id: 1"), addEachToSet("a", Arrays.asList(7, 7, 9, 2))); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: [6, 5, 4, 3, 2, 1, 7, 9]")); collection.updateOne(json("_id: 1"), addEachToSet("a", Arrays.asList(12, 13, 12))); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: [6, 5, 4, 3, 2, 1, 7, 9, 12, 13]")); }
private void addNewAnnotations(long entryId, List<Document> annotationDocumentList, boolean isVersioned) throws CatalogDBException { Document queryDocument = new Document(PRIVATE_UID, entryId); if (isVersioned) { queryDocument.append(LAST_OF_VERSION, true); } Bson push = Updates.addEachToSet(AnnotationSetParams.ANNOTATION_SETS.key(), annotationDocumentList); QueryResult<UpdateResult> update = getCollection().update(queryDocument, push, new QueryOptions("multi", true)); if (update.first().getModifiedCount() < 1) { throw new CatalogDBException("Could not add new annotations"); } }
bsons.add(resumeStageLoad ? addToSet(fieldName, binaryList.get(0)) : push(fieldName, binaryList.get(0))); } else { bsons.add(resumeStageLoad ? addEachToSet(fieldName, binaryList) : pushEach(fieldName, binaryList)); bsons.add(addEachToSet(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyFileValue)); bsons.add(setOnInsert(StageDocumentToVariantConverter.END_FIELD, id.get(StageDocumentToVariantConverter.END_FIELD))); bsons.add(setOnInsert(StageDocumentToVariantConverter.REF_FIELD, id.get(StageDocumentToVariantConverter.REF_FIELD)));
if (newVariant) { Document variantDocument = variantConverter.convertToStorageType(emptyVar); updates.add(addEachToSet(IDS_FIELD, ids)); for (Map.Entry<String, Object> entry : variantDocument.entrySet()) { if (!entry.getKey().equals("_id") && !entry.getKey().equals(STUDIES_FIELD) && !entry.getKey().equals(IDS_FIELD)) { List<Bson> mergeUpdates = new LinkedList<>(); if (!ids.isEmpty()) { mergeUpdates.add(addEachToSet(IDS_FIELD, ids)); List sampleIds = getListFromDocument(gts, gt); if (resume) { mergeUpdates.add(addEachToSet(STUDIES_FIELD + ".$." + GENOTYPES_FIELD + '.' + gt, sampleIds)); } else { mergeUpdates.add(pushEach(STUDIES_FIELD + ".$." + GENOTYPES_FIELD + '.' + gt, sampleIds)); mergeUpdates.add(addEachToSet(STUDIES_FIELD + ".$." + ALTERNATES_FIELD, secondaryAlternates)); mergeUpdates.add(addEachToSet(STUDIES_FIELD + ".$." + FILES_FIELD, fileDocuments)); } else { mergeUpdates.add(pushEach(STUDIES_FIELD + ".$." + FILES_FIELD, fileDocuments));
studiesCollection.update( eq("_id", studyId), addEachToSet("attributes." + LOADED_GENOTYPES.key(), new ArrayList<>(result.getGenotypes())), null);