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

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

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

Updates.unset介绍

[英]Creates an update that deletes the field with the given name.
[中]创建一个更新,删除具有给定名称的字段。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
public void upgrade() {
  if (clusterConfigService.get(MigrationCompleted.class) != null) {
    LOG.debug("Migration already done.");
    return;
  }
  // Do not overwrite an existing default index config
  boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null;
  final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
  final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE));
  for (final Document document : documents) {
    final ObjectId id = document.getObjectId(FIELD_ID);
    final String idString = id.toHexString();
    final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false);
    if (!defaultDone && isDefault) {
      defaultDone = true;
      clusterConfigService.write(DefaultIndexSetConfig.create(idString));
    }
    final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount();
    if (modifiedCount > 0) {
      LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
      builder.add(idString);
    } else {
      LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
    }
  }
  clusterConfigService.write(MigrationCompleted.create(builder.build()));
}

代码示例来源:origin: com.holon-platform.mongo/holon-datastore-mongo-core

updates.add(Updates.unset(fieldName));
} else {

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

private void removePrivateVariableMap(long entryId, Map<String, String> privateVariableMapToSet, boolean isVersioned)
    throws CatalogDBException {
  Document queryDocument = new Document(PRIVATE_UID, entryId);
  if (isVersioned) {
    queryDocument.append(LAST_OF_VERSION, true);
  }
  for (Map.Entry<String, String> entry : privateVariableMapToSet.entrySet()) {
    // We only want to remove the private variable map if it is not currently in use by any annotation set
    queryDocument.append(AnnotationSetParams.VARIABLE_SET_ID.key(), new Document("$ne", Long.parseLong(entry.getKey())));
    Bson unset = Updates.unset(AnnotationSetParams.PRIVATE_VARIABLE_SET_MAP.key() + "." + entry.getKey());
    QueryResult<UpdateResult> update = getCollection().update(queryDocument, unset, new QueryOptions());
    if (update.first().getModifiedCount() < 1 && update.first().getMatchedCount() == 1) {
      throw new CatalogDBException("Could not remove private map information");
    }
  }
}

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

Bson updateStage = combine(
      pull(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyId.toString()),
      unset(studyId.toString()));
  if (ids.size() == batchSize || !cursor.hasNext()) {
    updatedStageDocuments += stageCollection.update(in("_id", ids), updateStage, new QueryOptions(MULTI, true))
studyUpdate.add(unset(String.valueOf(studyId) + '.' + fileId));

代码示例来源:origin: atlanmod/NeoEMF

@Override
public void removeContainer(Id id) {
  checkNotNull(id, "id");
  final String ownerId = idConverter.convert(id);
  final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(ModelDocument.F_CONTAINER));
  final Bson update = unset(ModelDocument.F_CONTAINER);
  documents.updateOne(filter, update);
}

代码示例来源:origin: de.otto.edison/edison-mongo

@Override
public String setValue(final String jobType,
           final String key,
           final String value) {
  final Document previous;
  if (value != null) {
    previous = collection.findOneAndUpdate(eq(ID, jobType), set(key, value), UPSERT);
  } else {
    previous = collection.findOneAndUpdate(eq(ID, jobType), unset(key), UPSERT);
  }
  return previous != null
      ? previous.getString("key")
      : null;
}

代码示例来源:origin: atlanmod/NeoEMF

@Override
public void removeValue(SingleFeatureBean feature) {
  checkNotNull(feature, "feature");
  final String ownerId = idConverter.convert(feature.owner());
  final String featureId = Integer.toString(feature.id());
  final String fieldName = concat(ModelDocument.F_SINGLE_FEATURE, featureId);
  final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName));
  final Bson update = unset(fieldName);
  documents.updateOne(filter, update);
}

代码示例来源:origin: otto-de/edison-microservice

@Override
public String setValue(final String jobType,
            final String key,
            final String value) {
  final Document previous;
  if (value != null) {
    previous = collection.findOneAndUpdate(eq(ID, jobType), set(key, value), UPSERT);
  } else {
    previous = collection.findOneAndUpdate(eq(ID, jobType), unset(key), UPSERT);
  }
  return previous != null
      ? previous.getString("key")
      : null;
}

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

List<Bson> updates = new LinkedList<>();
for (Integer fileId : fileIds) {
  updates.add(unset(studyId + "." + fileId));

代码示例来源: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: atlanmod/NeoEMF

@Override
public void removeAllValues(SingleFeatureBean feature) {
  checkNotNull(feature, "feature");
  final String ownerId = idConverter.convert(feature.owner());
  final String fieldName = ModelDocument.F_MANY_FEATURE;
  final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName));
  final Bson update = unset(fieldName);
  documents.updateOne(filter, update);
}

代码示例来源:origin: org.graylog2/graylog2-server

@Override
public void upgrade() {
  if (clusterConfigService.get(MigrationCompleted.class) != null) {
    LOG.debug("Migration already done.");
    return;
  }
  // Do not overwrite an existing default index config
  boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null;
  final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
  final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE));
  for (final Document document : documents) {
    final ObjectId id = document.getObjectId(FIELD_ID);
    final String idString = id.toHexString();
    final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false);
    if (!defaultDone && isDefault) {
      defaultDone = true;
      clusterConfigService.write(DefaultIndexSetConfig.create(idString));
    }
    final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount();
    if (modifiedCount > 0) {
      LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
      builder.add(idString);
    } else {
      LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
    }
  }
  clusterConfigService.write(MigrationCompleted.create(builder.build()));
}

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

public static long cleanStageCollection(MongoDBCollection stageCollection, int studyId, int fileId) {
    //Delete those studies that have duplicated variants. Those are not inserted, so they are not new variants.
    long modifiedCount = stageCollection.update(
        and(exists(studyId + "." + fileId + ".1"), exists(studyId + "." + NEW_STUDY_FIELD, false)),
        unset(Integer.toString(studyId)),
        new QueryOptions(MongoDBCollection.MULTI, true)).first().getModifiedCount();
    modifiedCount += stageCollection.update(
        exists(studyId + "." + fileId),
        combine(
//                        unset(studyId + "." + fileId),
            set(studyId + "." + fileId, null),
            set(studyId + "." + NEW_STUDY_FIELD, false)
        ), new QueryOptions(MongoDBCollection.MULTI, true)).first().getModifiedCount();
    return modifiedCount;
  }

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

@Test
public void testUpdateUnset() throws Exception {
  Document obj = json("_id: 1, a: 1, b: null, c: 'value'");
  collection.insertOne(obj);
  assertMongoWriteException(() -> collection.updateOne(obj, json("$unset: {_id: ''}")),
    66, "Performing an update on the path '_id' would modify the immutable field '_id'");
  collection.updateOne(obj, json("$unset: {a:'', b:''}"));
  assertThat(collection.find().first()).isEqualTo(json("_id: 1, c: 'value'"));
  collection.updateOne(obj, Updates.unset("c.y"));
  assertThat(collection.find().first()).isEqualTo(json("_id: 1, c: 'value'"));
  collection.replaceOne(json("_id: 1"), json("a: {b: 'foo', c: 'bar'}"));
  collection.updateOne(json("_id: 1"), json("$unset: {'a.b':1}"));
  assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: {c: 'bar'}"));
}

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

@Override
public QueryResult<Long> deleteConfig(String userId, String name) throws CatalogDBException {
  long startTime = startQuery();
  // Insert the config
  Bson bsonQuery = Filters.and(
      Filters.eq(QueryParams.ID.key(), userId),
      Filters.exists(QueryParams.CONFIGS.key() + "." + name)
  );
  Bson update = Updates.unset(QueryParams.CONFIGS.key() + "." + name);
  QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null);
  if (queryResult.first().getModifiedCount() == 0) {
    throw new CatalogDBException("Could not delete " + name + " configuration ");
  }
  QueryResult<User> userQueryResult = get(userId, new QueryOptions(), "");
  if (userQueryResult.getNumResults() == 0) {
    throw new CatalogDBException("Internal error: Could not retrieve user " + userId + " information");
  }
  return endQuery("Delete config", startTime, Arrays.asList(queryResult.first().getModifiedCount()));
}

相关文章