com.mongodb.client.model.Updates.addEachToSet()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中com.mongodb.client.model.Updates.addEachToSet()方法的一些代码示例,展示了Updates.addEachToSet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Updates.addEachToSet()方法的具体详情如下:
包路径:com.mongodb.client.model.Updates
类名称:Updates
方法名:addEachToSet

Updates.addEachToSet介绍

[英]Creates an update that adds each of the given values to the array value of the field with the given name, unless the value is already present, in which case it does nothing
[中]创建一个更新,将每个给定值添加到具有给定名称的字段的数组值中,除非该值已经存在,在这种情况下,它不会执行任何操作

代码示例

代码示例来源:origin: opencb/opencga

@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());
}

代码示例来源:origin: de.bwaldvogel/mongo-java-server-test-common

@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]"));
}

代码示例来源:origin: opencb/opencga

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");
  }
}

代码示例来源:origin: opencb/opencga

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)));

代码示例来源:origin: opencb/opencga

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));

代码示例来源:origin: opencb/opencga

studiesCollection.update(
    eq("_id", studyId),
    addEachToSet("attributes." + LOADED_GENOTYPES.key(), new ArrayList<>(result.getGenotypes())),
    null);

相关文章