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

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

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

Updates.pull介绍

[英]Creates an update that removes all instances of the given value from the array value of the field with the given name.
[中]创建一个更新,从具有给定名称的字段的数组值中删除给定值的所有实例。

代码示例

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

protected void unmarkPermissionRule(MongoDBCollection collection, long studyId, String permissionRuleId) {
  Bson query = new Document()
      .append(PRIVATE_STUDY_ID, studyId)
      .append(PERMISSION_RULES_APPLIED, permissionRuleId);
  Bson update = Updates.pull(PERMISSION_RULES_APPLIED, permissionRuleId);
  collection.update(query, update, new QueryOptions("multi", true));
}

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

pull(DocumentToVariantConverter.STUDIES_FIELD + ".$." + FILES_FIELD,
        in(FILEID_FIELD, fileIds)));
for (String gt : sc.getAttributes().getAsStringList(LOADED_GENOTYPES.key())) {

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

private QueryResult<UpdateResult> removeStudyFromVariants(int studyId, Bson query, long timestamp) {
  // { $pull : { files : {  sid : <studyId> } } }
  Bson update = combine(
      pull(DocumentToVariantConverter.STUDIES_FIELD, eq(STUDYID_FIELD, studyId)),
      pull(DocumentToVariantConverter.STATS_FIELD, eq(DocumentToVariantStatsConverter.STUDY_ID, studyId)),
      getSetIndexNotSynchronized(timestamp)
  );
  logger.debug("removeStudy: query = {}", query.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()));
  logger.debug("removeStudy: update = {}", update.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()));
  QueryResult<UpdateResult> result = variantsCollection.update(query, update, new QueryOptions(MULTI, true));
  logger.debug("removeStudy: matched  = {}", result.first().getMatchedCount());
  logger.debug("removeStudy: modified = {}", result.first().getModifiedCount());
  return result;
}

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

private void removeAnnotationSet(long entryId, String annotationSetId, boolean isVersioned) throws CatalogDBException {
  Document queryDocument = new Document(PRIVATE_UID, entryId);
  if (isVersioned) {
    queryDocument.append(LAST_OF_VERSION, true);
  }
  Bson pull = Updates.pull(AnnotationSetParams.ANNOTATION_SETS.key(),
      new Document(AnnotationSetParams.ANNOTATION_SET_NAME.key(), annotationSetId));
  QueryResult<UpdateResult> update = getCollection().update(queryDocument, pull, new QueryOptions("multi", true));
  if (update.first().getModifiedCount() < 1) {
    throw new CatalogDBException("Could not delete the annotation set");
  }
}

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

))
    .append("$isolated", 1);
Bson pull = Updates.pull("groups.$.userIds", user);

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

ids.add(cursor.next().getString("_id"));
Bson updateStage = combine(
    pull(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyId.toString()),
    unset(studyId.toString()));
if (ids.size() == batchSize || !cursor.hasNext()) {

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

@Test
public void testUpdatePull() throws Exception {
  Document obj = json("_id: 1");
  collection.insertOne(obj);
  // pull from non-existing field
  collection.updateOne(obj, json("$pull: {field1: 'value2', field2: 'value3'}"));
  assertThat(collection.find(obj).first()).isEqualTo(obj);
  // pull from non-array
  collection.updateOne(obj, set("field", "value"));
  assertMongoWriteException(() -> collection.updateOne(obj, pull("field", "value")),
    2, "Cannot apply $pull to a non-array value");
  // pull standard
  collection.updateOne(obj, json("$set: {field: ['value1', 'value2', 'value1']}"));
  collection.updateOne(obj, pull("field", "value1"));
  assertThat(collection.find(obj).first().get("field")).isEqualTo(Collections.singletonList("value2"));
  // pull with multiple fields
  collection.updateOne(obj, json("$set: {field1: ['value1', 'value2', 'value1']}"));
  collection.updateOne(obj, json("$set: {field2: ['value3', 'value3', 'value1']}"));
  collection.updateOne(obj, json("$pull: {field1: 'value2', field2: 'value3'}"));
  assertThat(collection.find(obj).first().get("field1")).isEqualTo(Arrays.asList("value1", "value1"));
  assertThat(collection.find(obj).first().get("field2")).isEqualTo(Collections.singletonList("value1"));
}

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

Bson combine = combine(pull(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyId.toString()), unset(studyId.toString()));
logger.debug("removeStudy: stage query = " + eq.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()));
logger.debug("removeStudy: stage update = " + combine.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()));

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

@Override
public QueryResult deleteFilter(String userId, String name) throws CatalogDBException {
  long startTime = startQuery();
  // Delete the filter
  Bson bsonQuery = Filters.and(
      Filters.eq(QueryParams.ID.key(), userId),
      Filters.eq(QueryParams.CONFIGS_FILTERS_NAME.key(), name)
  );
  Bson update = Updates.pull(QueryParams.CONFIGS_FILTERS.key(), new Document(FilterParams.NAME.key(), name));
  QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null);
  if (queryResult.first().getModifiedCount() == 0) {
    throw new CatalogDBException("Internal error: Filter " + name + " could not be removed");
  }
  return endQuery("Delete filter", startTime, Arrays.asList(queryResult.first().getModifiedCount()));
}

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

@Override
public QueryResult<VariableSet> deleteVariableSet(long variableSetId, QueryOptions queryOptions, String user)
    throws CatalogDBException, CatalogAuthorizationException {
  long startTime = startQuery();
  QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, queryOptions, user);
  checkVariableSetInUse(variableSetId);
  Bson query = Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId);
  Bson operation = Updates.pull("variableSets", Filters.eq(PRIVATE_UID, variableSetId));
  QueryResult<UpdateResult> update = studyCollection.update(query, operation, null);
  if (update.first().getModifiedCount() == 0) {
    throw CatalogDBException.uidNotFound("VariableSet", variableSetId);
  }
  return endQuery("Delete VariableSet", startTime, variableSet);
}

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

@Override
public QueryResult<VariableSet> removeFieldFromVariableSet(long variableSetId, String name, String user)
    throws CatalogDBException, CatalogAuthorizationException {
  long startTime = startQuery();
  QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, new QueryOptions(), user);
  checkVariableInVariableSet(variableSet.first(), name);
  Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId);
  Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(),
      Filters.eq("id", name));
  QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null);
  if (queryResult.first().getModifiedCount() != 1) {
    throw new CatalogDBException("Remove field from Variable Set. Could not remove the field " + name
        + " from the variableSet id " + variableSetId);
  }
  // Remove all the annotations from that field
  dbAdaptorFactory.getCatalogSampleDBAdaptor().removeAnnotationField(variableSetId, name);
  dbAdaptorFactory.getCatalogCohortDBAdaptor().removeAnnotationField(variableSetId, name);
  dbAdaptorFactory.getCatalogIndividualDBAdaptor().removeAnnotationField(variableSetId, name);
  dbAdaptorFactory.getCatalogFamilyDBAdaptor().removeAnnotationField(variableSetId, name);
  dbAdaptorFactory.getCatalogFileDBAdaptor().removeAnnotationField(variableSetId, name);
  return endQuery("Remove field from Variable Set", startTime, getVariableSet(variableSetId, null));
}

相关文章