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

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

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

Aggregates.limit介绍

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

代码示例

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

private static void addSkipAndLimit(final Collection<Bson> pipeline,
    final int skip,
    final int limit,
    final boolean isCount) {
  if (!isCount) {
    pipeline.add(Aggregates.skip(skip));
    pipeline.add(Aggregates.limit(limit + 1));
  }
}

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

private static void addSkipAndLimit(final Collection<Bson> pipeline,
    final int skip,
    final int limit,
    final boolean isCount) {
  if (!isCount) {
    pipeline.add(Aggregates.skip(skip));
    pipeline.add(Aggregates.limit(limit + 1));
  }
}

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

public static Bson getLimit(QueryOptions options) {
  if (options.getInt(QueryOptions.LIMIT) > 0) {
    return Aggregates.limit(options.getInt(QueryOptions.LIMIT));
  }
  return null;
}

代码示例来源:origin: org.opencb.cellbase/cellbase-lib

} else {
  Bson limit = Aggregates.limit(options.getInt("limit", 10));
  group = Aggregates.group(id, Accumulators.addToSet("features", "$" + featureIdField));

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

} else {
  Bson limit = Aggregates.limit(options.getInt("limit", 10));
  group = Aggregates.group(id, Accumulators.addToSet("features", "$" + featureIdField));

代码示例来源: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: org.opencb.cellbase/cellbase-lib

protected QueryResult groupBy(Bson query, String groupByField, String featureIdField, QueryOptions options) {
  if (groupByField == null || groupByField.isEmpty()) {
    return new QueryResult();
  }
  if (groupByField.contains(",")) {
    // call to multiple groupBy if commas are present
    return groupBy(query, Arrays.asList(groupByField.split(",")), featureIdField, options);
  } else {
    Bson match = Aggregates.match(query);
    Bson project = Aggregates.project(Projections.include(groupByField, featureIdField));
    Bson group;
    if (options.getBoolean("count", false)) {
      group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
      return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
    } else {
      // Limit the documents passed if count is false
      Bson limit = Aggregates.limit(options.getInt("limit", 10));
      group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + featureIdField));
      // TODO change the default "_id" returned by mongodb to id
      return mongoDBCollection.aggregate(Arrays.asList(match, limit, project, group), options);
    }
  }
}

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

protected QueryResult groupBy(Bson query, String groupByField, String featureIdField, QueryOptions options) {
  if (groupByField == null || groupByField.isEmpty()) {
    return new QueryResult();
  }
  if (groupByField.contains(",")) {
    // call to multiple groupBy if commas are present
    return groupBy(query, Arrays.asList(groupByField.split(",")), featureIdField, options);
  } else {
    Bson match = Aggregates.match(query);
    Bson project = Aggregates.project(Projections.include(groupByField, featureIdField));
    Bson group;
    if (options.getBoolean("count", false)) {
      group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
      return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
    } else {
      // Limit the documents passed if count is false
      Bson limit = Aggregates.limit(options.getInt("limit", 10));
      group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + featureIdField));
      // TODO change the default "_id" returned by mongodb to id
      return mongoDBCollection.aggregate(Arrays.asList(match, limit, project, group), options);
    }
  }
}

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

@Nonnull
@Nonnegative
@Override
public Optional<Integer> sizeOfValue(SingleFeatureBean feature) {
  checkNotNull(feature, "feature");
  final String ownerId = idConverter.convert(feature.owner());
  final String fieldName = ModelDocument.F_MANY_FEATURE;
  final String fieldSize = "size";
  final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName));
  final Bson projection = computed(fieldSize, new Document(QueryOperators.SIZE, concat('$' + fieldName, Integer.toString(feature.id()))));
  final List<Bson> pipeline = Arrays.asList(
      match(filter),
      limit(1),
      project(projection)
  );
  try {
    final AggregateIterable<BasicDBObject> aggregate = documents.aggregate(pipeline, BasicDBObject.class);
    return MoreIterables.onlyElement(aggregate).map(o -> o.getInt(fieldSize));
  }
  catch (MongoCommandException e) {
    // FIXME Don't use an exception to determine the presence of an index
    if (e.getErrorCode() != 17124) { // "the $size operator requires an list" when index does not exist in collection
      throw e;
    }
    return Optional.empty();
  }
}

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

definition.getLimit().ifPresent(l -> pipeline.add(Aggregates.limit(l)));

代码示例来源: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;
}

相关文章