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

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

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

Updates.inc介绍

[英]Creates an update that increments the value of the field with the given name by the given value.
[中]创建一个更新,将具有给定名称的字段的值按给定值递增。

代码示例

代码示例来源:origin: com.cybermkd/MongodbPlugin

public MongoQuery inc(String key, Number value) {
  data.add(Updates.inc(key, value));
  return this;
}

代码示例来源:origin: T-baby/MongoDB-Plugin

public MongoQuery inc(String key, Number value) {
  data.add(Updates.inc(key, value));
  return this;
}

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

public long getNewAutoIncrementId(String field) { //, MongoDBCollection metaCollection
  Bson query = METADATA_QUERY;
  Document projection = new Document(field, true);
  Bson inc = Updates.inc(field, 1L);
  QueryOptions queryOptions = new QueryOptions("returnNew", true);
  QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions);
  return result.getResult().get(0).getLong(field);
}

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

@Deprecated
  static long getNewAutoIncrementId(String field, MongoDBCollection metaCollection) {

    Bson query = Filters.eq(PRIVATE_ID, MongoDBAdaptorFactory.METADATA_OBJECT_ID);
    Document projection = new Document(field, true);
    Bson inc = Updates.inc(field, 1);
    QueryOptions queryOptions = new QueryOptions("returnNew", true);
    QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions);
//        return (int) Float.parseFloat(result.getResult().get(0).get(field).toString());
    return result.getResult().get(0).getInteger(field);
  }

代码示例来源:origin: org.nuxeo.ecm.core/nuxeo-core-storage-mongodb

protected synchronized Long getNextSequenceId() {
  if (sequenceLeft == 0) {
    // allocate a new sequence block
    // the database contains the last value from the last block
    Bson filter = Filters.eq(MONGODB_ID, COUNTER_NAME_UUID);
    Bson update = Updates.inc(COUNTER_FIELD, Long.valueOf(sequenceBlockSize));
    Document idCounter = countersColl.findOneAndUpdate(filter, update,
        new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
    if (idCounter == null) {
      throw new NuxeoException("Repository id counter not initialized");
    }
    sequenceLeft = sequenceBlockSize;
    sequenceLastValue = ((Long) idCounter.get(COUNTER_FIELD)).longValue() - sequenceBlockSize;
  }
  sequenceLeft--;
  sequenceLastValue++;
  return Long.valueOf(sequenceLastValue);
}

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

/***
   * This method is called every time a file has been inserted, modified or deleted to keep track of the current study size.
   *
   * @param studyId   Study Identifier
   * @param size disk usage of a new created, updated or deleted file belonging to studyId. This argument
   *                  will be > 0 to increment the size field in the study collection or < 0 to decrement it.
   * @throws CatalogDBException An exception is launched when the update crashes.
   */
  public void updateDiskUsage(long studyId, long size) throws CatalogDBException {
    Bson query = new Document(QueryParams.UID.key(), studyId);
    Bson update = Updates.inc(QueryParams.SIZE.key(), size);
    if (studyCollection.update(query, update, null).getNumTotalResults() == 0) {
      throw new CatalogDBException("CatalogMongoStudyDBAdaptor updateDiskUsage: Couldn't update the size field of"
          + " the study " + studyId);
    }
  }
}

代码示例来源:origin: org.nuxeo.ecm.platform/nuxeo-platform-directory-mongodb

if (autoincrementId) {
  Document filter = MongoDBSerializationHelper.fieldMapToBson(MONGODB_ID, directoryName);
  Bson update = Updates.inc(MONGODB_SEQ, 1L);
  FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
  Long longId = getCountersCollection().findOneAndUpdate(filter, update, options).getLong(MONGODB_SEQ);

代码示例来源:origin: creactiviti/piper

@Override
public long decrement(String aCounterName) {
 Document doc = collection
   .findOneAndUpdate(
     eq("_id", aCounterName),
     inc(DSL.VALUE, -1),
     new FindOneAndUpdateOptions()
       .returnDocument(AFTER)
       .projection(include(DSL.VALUE))
   );
 if (doc == null) {
  throw new IllegalArgumentException("Counter not found: " + aCounterName);
 }
 return doc.getLong(DSL.VALUE);
}

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

private int generateId(String idType, boolean retry) throws StorageEngineException {
    String field = COUNTERS_FIELD + '.' + idType;
    Document projection = new Document(field, true);
    Bson inc = Updates.inc(field, 1);
    QueryOptions queryOptions = new QueryOptions("returnNew", true);
    QueryResult<Document> result = collection.findAndUpdate(QUERY, projection, null, inc, queryOptions);
    if (result.first() == null) {
      if (retry) {
        ensureProjectMetadataExists();
        return generateId(idType, false);
      } else {
        throw new StorageEngineException("Error creating new ID. Project Metadata not found");
      }
    } else {
      Document document = result.getResult().get(0);
      Document counters = document.get(COUNTERS_FIELD, Document.class);
      Integer id = counters.getInteger(idType);
//            System.out.println("New ID " + idType + " : " + id);
      return id;
    }
  }

相关文章