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

x33g5p2x  于2022-01-16 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(264)

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

Aggregates.group介绍

[英]Creates a $group pipeline stage for the specified filter
[中]为指定的筛选器创建$group pipeline阶段

代码示例

代码示例来源:origin: org.mongodb/mongo-java-driver

/**
 * Creates a $group pipeline stage for the specified filter
 *
 * @param <TExpression>     the expression type
 * @param id                the id expression for the group, which may be null
 * @param fieldAccumulators zero or more field accumulator pairs
 * @return the $group pipeline stage
 * @mongodb.driver.manual reference/operator/aggregation/group/ $group
 * @mongodb.driver.manual meta/aggregation-quick-reference/#aggregation-expressions Expressions
 */
public static <TExpression> Bson group(@Nullable final TExpression id, final BsonField... fieldAccumulators) {
  return group(id, asList(fieldAccumulators));
}

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

public MongoAggregation group(Bson bson) {
  pipeline.add(Aggregates.group(bson));
  return this;
}

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

public MongoAggregation group(Bson bson) {
  pipeline.add(Aggregates.group(bson));
  return this;
}

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

public MongoAggregation group(String fieldName, MongoAccumulator accumulator) {
  pipeline.add(Aggregates.group(fieldName, accumulator.getAccumulators()));
  return this;
}

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

public MongoAggregation group(String fieldName, MongoAccumulator accumulator) {
  pipeline.add(Aggregates.group(fieldName, accumulator.getAccumulators()));
  return this;
}

代码示例来源:origin: org.mongodb/mongodb-driver-core

/**
 * Creates a $group pipeline stage for the specified filter
 *
 * @param <TExpression>     the expression type
 * @param id                the id expression for the group, which may be null
 * @param fieldAccumulators zero or more field accumulator pairs
 * @return the $group pipeline stage
 * @mongodb.driver.manual reference/operator/aggregation/group/ $group
 * @mongodb.driver.manual meta/aggregation-quick-reference/#aggregation-expressions Expressions
 */
public static <TExpression> Bson group(@Nullable final TExpression id, final BsonField... fieldAccumulators) {
  return group(id, asList(fieldAccumulators));
}

代码示例来源:origin: org.apache.rya/mongodb.rya

/**
 * Add a $group step to filter out redundant solutions.
 * @return True if the distinct operation was successfully appended.
 */
public boolean distinct() {
  List<String> key = new LinkedList<>();
  for (String varName : bindingNames) {
    key.add(hashFieldExpr(varName));
  }
  List<BsonField> reduceOps = new LinkedList<>();
  for (String field : FIELDS) {
    reduceOps.add(new BsonField(field, new Document("$first", "$" + field)));
  }
  pipeline.add(Aggregates.group(new Document("$concat", key), reduceOps));
  return true;
}

代码示例来源:origin: apache/incubator-rya

/**
 * Add a $group step to filter out redundant solutions.
 * @return True if the distinct operation was successfully appended.
 */
public boolean distinct() {
  final List<String> key = new LinkedList<>();
  for (final String varName : bindingNames) {
    key.add(hashFieldExpr(varName));
  }
  final List<BsonField> reduceOps = new LinkedList<>();
  for (final String field : FIELDS) {
    reduceOps.add(new BsonField(field, new Document("$first", "$" + field)));
  }
  pipeline.add(Aggregates.group(new Document("$concat", key), reduceOps));
  return true;
}

代码示例来源:origin: org.opencb.commons/commons-datastore-mongodb

public static List<Bson> createGroupBy(Bson query, String groupByField, String idField, boolean count) {
  if (groupByField == null || groupByField.isEmpty()) {
    return new ArrayList<>();
  }
  if (groupByField.contains(",")) {
    // call to multiple createGroupBy if commas are present
    return createGroupBy(query, Arrays.asList(groupByField.split(",")), idField, count);
  } else {
    Bson match = Aggregates.match(query);
    Bson project = Aggregates.project(Projections.include(groupByField, idField));
    Bson group;
    if (count) {
      group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
    } else {
      group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + idField));
    }
    return Arrays.asList(match, project, group);
  }
}

代码示例来源:origin: org.eclipse.ditto/ditto-services-thingsearch-persistence

private static void addCountStage(final Collection<Bson> pipeline, final boolean isCount) {
  if (isCount) {
    pipeline.add(group(new BsonDocument(FIELD_ID, BsonNull.VALUE), new BsonField(COUNT_RESULT_NAME,
        new BsonDocument(SUM_GROUPING, BSON_INT_1))));
  }
}

代码示例来源:origin: eclipse/ditto

private static void addCountStage(final Collection<Bson> pipeline, final boolean isCount) {
  if (isCount) {
    pipeline.add(group(new BsonDocument(FIELD_ID, BsonNull.VALUE), new BsonField(COUNT_RESULT_NAME,
        new BsonDocument(SUM_GROUPING, BSON_INT_1))));
  }
}

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

private long getDiskUsageByStudy(int studyId) {
  List<Bson> operations = new ArrayList<>();
  operations.add(Aggregates.match(Filters.eq(PRIVATE_STUDY_ID, studyId)));
  operations.add(Aggregates.group("$" + PRIVATE_STUDY_ID, Accumulators.sum("size", "$diskUsage")));
  QueryResult<Document> aggregate = dbAdaptorFactory.getCatalogFileDBAdaptor().getCollection()
      .aggregate(operations, null);
  if (aggregate.getNumResults() == 1) {
    Object size = aggregate.getResult().get(0).get("size");
    if (size instanceof Integer) {
      return ((Integer) size).longValue();
    } else if (size instanceof Long) {
      return ((Long) size);
    } else {
      return Long.parseLong(size.toString());
    }
  } else {
    return 0;
  }
}

代码示例来源:origin: ysrc/Liudao

/**
 * 最大统计
 *
 * @param collectionName
 * @param match
 * @param maxField
 * @return
 */
public Object max(String collectionName, Document match, String maxField) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group(null, Accumulators.max("_max", "$" + maxField))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return first.get("_max");
  }
  return null;
}

代码示例来源:origin: ysrc/Liudao

/**
 * 平均统计
 *
 * @param collectionName
 * @param match
 * @param avgField
 * @return
 */
public Double avg(String collectionName, Document match, String avgField) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group(null, Accumulators.avg("_avg", "$" + avgField))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return first.getDouble("_avg");
  }
  return null;
}

代码示例来源:origin: ysrc/Liudao

/**
 * 最小统计
 *
 * @param collectionName
 * @param match
 * @param minField
 * @return
 */
public Object min(String collectionName, Document match, String minField) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group(null, Accumulators.min("_min", "$" + minField))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return first.get("_min");
  }
  return null;
}

代码示例来源:origin: ysrc/Liudao

/**
 * 获取不一样的记录
 *
 * @param collectionName
 * @param match
 * @param distinctField
 * @return
 */
public List distinct(String collectionName, Document match, String distinctField) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group(null, addToSet("_array", "$" + distinctField))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return (List) first.get("_array");
  }
  return null;
}

代码示例来源:origin: ysrc/Liudao

/**
 * 最近统计
 *
 * @param collectionName
 * @param match
 * @param lastField
 * @param sort
 * @return
 */
public Object last(String collectionName, Document match, String lastField, Document sort) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , sort(sort)
          , group(null, Accumulators.last("_last", "$" + lastField))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return first.get("_last");
  }
  return null;
}

代码示例来源:origin: epam/DLab

private Bson groupCriteria() {
  return Aggregates.group(getGroupingFields(
      MongoKeyWords.DLAB_USER,
      MongoKeyWords.DLAB_ID,
      MongoKeyWords.RESOURCE_TYPE,
      MongoKeyWords.METER_CATEGORY,
      MongoKeyWords.CURRENCY_CODE),
      Accumulators.sum(MongoKeyWords.COST, MongoKeyWords.prepend$(MongoKeyWords.COST)),
      Accumulators.min(MongoKeyWords.USAGE_FROM, MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY)),
      Accumulators.max(MongoKeyWords.USAGE_TO, MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY))
  );
}

代码示例来源:origin: org.eclipse.ditto/ditto-services-thingsearch-persistence

private static Bson createGroupStage() {
  return group(new BsonDocument(FIELD_ID, new BsonString(ID_VARIABLE)),
      new BsonField(FIELD_NAMESPACE, createProjectionDocument(FIRST_PROJECTION, NAMESPACE_VARIABLE)),
      new BsonField(FIELD_ATTRIBUTES, createProjectionDocument(FIRST_PROJECTION, FIELD_ATTRIBUTES_VARIABLE)),
      new BsonField(FIELD_FEATURES, createProjectionDocument(FIRST_PROJECTION, FIELD_FEATURES_VARIABLE)),
      new BsonField(FIELD_INTERNAL, createProjectionDocument(PUSH_PROJECTION, FIELD_INTERNAL_VARIABLE)),
      new BsonField(FIELD_DELETED, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_VARIABLE)),
      new BsonField(FIELD_REVISION, createProjectionDocument(FIRST_PROJECTION, FIELD_REVISION_VARIABLE)));
}

代码示例来源:origin: eclipse/ditto

private static Bson createGroupStage() {
  return group(new BsonDocument(FIELD_ID, new BsonString(ID_VARIABLE)),
      new BsonField(FIELD_NAMESPACE, createProjectionDocument(FIRST_PROJECTION, NAMESPACE_VARIABLE)),
      new BsonField(FIELD_ATTRIBUTES, createProjectionDocument(FIRST_PROJECTION, FIELD_ATTRIBUTES_VARIABLE)),
      new BsonField(FIELD_FEATURES, createProjectionDocument(FIRST_PROJECTION, FIELD_FEATURES_VARIABLE)),
      new BsonField(FIELD_INTERNAL, createProjectionDocument(PUSH_PROJECTION, FIELD_INTERNAL_VARIABLE)),
      new BsonField(FIELD_DELETED, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_VARIABLE)),
      new BsonField(FIELD_DELETED_FLAG, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_FLAG_VARIABLE)),
      new BsonField(FIELD_REVISION, createProjectionDocument(FIRST_PROJECTION, FIELD_REVISION_VARIABLE)));
}

相关文章