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

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

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

Aggregates.sort介绍

[英]Creates a $sort pipeline stage for the specified sort specification
[中]为指定的排序规范创建$sort pipeline stage

代码示例

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

private static void addSortingStage(final Collection<Bson> pipeline, final Collection<SortOption> sortOptions,
    final boolean isCount) {
  if (!isCount && !sortOptions.isEmpty()) {
    pipeline.add(sort(getSortOptionsAsBson(sortOptions)));
  }
}

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

private static void addSortingStage(final Collection<Bson> pipeline, final Collection<SortOption> sortOptions,
    final boolean isCount) {
  if (!isCount && !sortOptions.isEmpty()) {
    pipeline.add(sort(getSortOptionsAsBson(sortOptions)));
  }
}

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

public static Bson getSort(QueryOptions options) {
  Object sortObject = options.get(QueryOptions.SORT);
  if (sortObject != null) {
    if (sortObject instanceof Bson) {
      return Aggregates.sort((Bson) sortObject);
    } else if (sortObject instanceof String) {
      String order = options.getString(QueryOptions.ORDER, "DESC");
      if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase("ASC") || order.equals("1")) {
        return Aggregates.sort(Sorts.ascending((String) sortObject));
      } else {
        return Aggregates.sort(Sorts.descending((String) sortObject));
      }
    }
  }
  return null;
}

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

@Override
public List<Context> getStack(String aStackId) {
 return collection
   .withDocumentClass(Context.class)
   .aggregate(ImmutableList.of(
     match(eq("stackId", aStackId)),
     sort(descending("createTime")),
     replaceRoot("$context")
   ))
   .into(new ArrayList<>());
}

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

/**
 * 根据统计字段计算统计结果(gte最小值)并排序
 *
 * @param collectionName 集合名
 * @param match          match条件
 * @param field          统计字段
 * @param minCount       最小值
 * @return
 */
public LinkedHashMap<String, Integer> sortMap(String collectionName, Document match, String field, int minCount) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group("$" + field, Accumulators.sum("_count", 1))
          , match(new Document("_count", new Document("$gte", minCount)))
          , sort(new Document("_count", -1))
      )
  );
  LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
  MongoCursor<Document> iterator = aggregate.iterator();
  while (iterator.hasNext()) {
    Document next = iterator.next();
    map.put(next.getString("_id"), next.getInteger("_count"));
  }
  return map;
}

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

protected QueryResult rank(MongoDBCollection collection, Bson query, String groupByField, String idField, int numResults, boolean asc) {
  if (groupByField == null || groupByField.isEmpty()) {
    return new QueryResult();
  }
  if (groupByField.contains(",")) {
    // call to multiple rank if commas are present
    return rank(collection, query, Arrays.asList(groupByField.split(",")), idField, numResults, asc);
  } else {
    Bson match = Aggregates.match(query);
    Bson project = Aggregates.project(Projections.include(groupByField, idField));
    Bson group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
    Bson sort;
    if (asc) {
      sort = Aggregates.sort(Sorts.ascending("count"));
    } else {
      sort = Aggregates.sort(Sorts.descending("count"));
    }
    Bson limit = Aggregates.limit(numResults);
    return collection.aggregate(Arrays.asList(match, project, group, sort, limit), new QueryOptions());
  }
}

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

protected QueryResult rank(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, int numResults,
              boolean asc) {
  if (groupByField == null || groupByField.isEmpty()) {
    return new QueryResult();
  }
  if (groupByField.size() == 1) {
    // if only one field then we call to simple rank
    return rank(collection, query, groupByField.get(0), idField, numResults, asc);
  } else {
    Bson match = Aggregates.match(query);
    // add all group-by fields to the projection together with the aggregation field name
    List<String> groupByFields = new ArrayList<>(groupByField);
    groupByFields.add(idField);
    Bson project = Aggregates.project(Projections.include(groupByFields));
    // _id document creation to have the multiple id
    Document id = new Document();
    for (String s : groupByField) {
      id.append(s, "$" + s);
    }
    Bson group = Aggregates.group(id, Accumulators.sum("count", 1));
    Bson sort;
    if (asc) {
      sort = Aggregates.sort(Sorts.ascending("count"));
    } else {
      sort = Aggregates.sort(Sorts.descending("count"));
    }
    Bson limit = Aggregates.limit(numResults);
    return collection.aggregate(Arrays.asList(match, project, group, sort, limit), new QueryOptions());
  }
}

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

private Bson sortCriteria() {
  return Aggregates.sort(Sorts.ascending(
      MongoKeyWords.prependId(MongoKeyWords.DLAB_USER),
      MongoKeyWords.prependId(MongoKeyWords.DLAB_ID),
      MongoKeyWords.prependId(MongoKeyWords.RESOURCE_TYPE),
      MongoKeyWords.prependId(MongoKeyWords.METER_CATEGORY)));
}

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

public MongoAggregation(MongoQuery query) {
  /*复用MongoQuery*/
  this.query = query;
  if (query.getQuery() != null && !query.getQuery().isEmpty()) {
    pipeline.add(Aggregates.match(Filters.and(query.getQuery())));
  }
  if (query.getSort() != null) {
    pipeline.add(Aggregates.sort(query.getSort()));
  }
  if (query.getSkip() > 0) {
    pipeline.add(Aggregates.skip(query.getSkip()));
  }
  if (query.getLimit() > 0) {
    pipeline.add(Aggregates.limit(query.getLimit()));
  }
}

代码示例来源: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: T-baby/MongoDB-Plugin

public MongoAggregation(MongoQuery query) {
  /*复用MongoQuery*/
  this.query = query;
  if (query.getQuery() != null && !query.getQuery().isEmpty()) {
    pipeline.add(Aggregates.match(Filters.and(query.getQuery())));
  }
  if (query.getSort() != null) {
    pipeline.add(Aggregates.sort(query.getSort()));
  }
  if (query.getSkip() > 0) {
    pipeline.add(Aggregates.skip(query.getSkip()));
  }
  if (query.getLimit() > 0) {
    pipeline.add(Aggregates.limit(query.getLimit()));
  }
}

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

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

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

definition.getSort().ifPresent(s -> pipeline.add(Aggregates.sort(s)));

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

@Override
public Context peek(String aStackId) {
 Context context = collection
   .withDocumentClass(Context.class)
   .aggregate(ImmutableList.of(
     match(eq("stackId", aStackId)),
     sort(descending("createTime")),
     limit(1),
     replaceRoot("$context")
   ))
   .first();
 if (context != null) {
  return context;
 }
 return null;
}

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

),
Aggregates.sort(Sorts.ascending(
    MongoKeyWords.prependId(MongoKeyWords.RESOURCE_NAME),
    MongoKeyWords.prependId(MongoKeyWords.METER_CATEGORY)))

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

));
pipeline.add(
    sort(new Document(ID + "." + USER, 1)
        .append(ID + "." + FIELD_DLAB_ID, 1)
        .append(ID + "." + DLAB_RESOURCE_TYPE, 1)

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

max(FIELD_USAGE_DATE_END, "$" + ReportLine.FIELD_USAGE_DATE)
    ),
    sort(new Document(FIELD_ID + "." + FIELD_DLAB_RESOURCE_ID, 1).append(FIELD_ID + "." + ReportLine
        .FIELD_PRODUCT, 1))
);

相关文章