热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MongoDBAggregates.group()方法详解与编程实例

本文整理了Java中com.mongodb.client.model.Aggregates.group()方法的一些代码示例,展示了Aggregates.group()的具体用法。这些代码示例主要来源

本文整理了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 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 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 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 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 key = new LinkedList<>();
for (String varName : bindingNames) {
key.add(hashFieldExpr(varName));
}
List 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 key = new LinkedList<>();
for (final String varName : bindingNames) {
key.add(hashFieldExpr(varName));
}
final List 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 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 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 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 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 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 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 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 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 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 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)));
}

推荐阅读
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 本文详细介绍了 Java 中 org.apache.xmlbeans.SchemaType 类的 getBaseEnumType() 方法,提供了多个代码示例,并解释了其在不同场景下的使用方法。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • andr ... [详细]
author-avatar
lrg冰天雪地789_444
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有