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

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

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

Aggregates.project介绍

[英]Creates a $project pipeline stage for the specified projection
[中]为指定的投影创建$project pipeline阶段

代码示例

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

public MongoAggregation projection() {
  pipeline.add(Aggregates.project(Projections.fields(projections)));
  return this;
}

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

public static Bson getProjection(QueryOptions options) {
  Bson projection = getProjection(null, options);
  return projection != null ? Aggregates.project(projection) : null;
}

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

public MongoAggregation projection() {
  pipeline.add(Aggregates.project(Projections.fields(projections)));
  return this;
}

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

private static Bson createSecondProjectionStage() {
  return project(new BsonDocument().append(FIELD_ID, BsonBoolean.TRUE));
}

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

private static Bson createSecondProjectionStage() {
  return project(new BsonDocument().append(FIELD_ID, BsonBoolean.TRUE));
}

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

/**
 * Create a pipeline query node based on a StatementPattern.
 * @param collection The collection of triples to query.
 * @param baseSP The leaf node in the query tree.
 */
public AggregationPipelineQueryNode(MongoCollection<Document> collection, StatementPattern baseSP) {
  this.collection = Preconditions.checkNotNull(collection);
  Preconditions.checkNotNull(baseSP);
  this.varToOriginalName = HashBiMap.create();
  StatementVarMapping mapping = new StatementVarMapping(baseSP, varToOriginalName);
  this.assuredBindingNames = new HashSet<>(mapping.varNames());
  this.bindingNames = new HashSet<>(mapping.varNames());
  this.pipeline = new LinkedList<>();
  this.pipeline.add(Aggregates.match(getMatchExpression(baseSP)));
  this.pipeline.add(Aggregates.project(mapping.getProjectExpression()));
}

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

/**
 * Create a pipeline query node based on a StatementPattern.
 * @param collection The collection of triples to query.
 * @param baseSP The leaf node in the query tree.
 */
public AggregationPipelineQueryNode(final MongoCollection<Document> collection, final StatementPattern baseSP) {
  this.collection = Preconditions.checkNotNull(collection);
  Preconditions.checkNotNull(baseSP);
  this.varToOriginalName = HashBiMap.create();
  final StatementVarMapping mapping = new StatementVarMapping(baseSP, varToOriginalName);
  this.assuredBindingNames = new HashSet<>(mapping.varNames());
  this.bindingNames = new HashSet<>(mapping.varNames());
  this.pipeline = new LinkedList<>();
  this.pipeline.add(Aggregates.match(getMatchExpression(baseSP)));
  this.pipeline.add(Aggregates.project(mapping.getProjectExpression()));
}

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

/**
 * Process given filter and returns the aggregation pipeline stages to add.
 * @param filter Filter
 * @param finalProjection Optional final projection
 * @return Aggregation pipeline stage
 */
private static List<Bson> buildAggregationFilter(BsonFilterExpression filter, Optional<Bson> finalProjection) {
  final List<Bson> pipeline = new LinkedList<>();
  if (filter.getPipeline().isPresent()) {
    final FilterAggregationPipeline fap = filter.getPipeline().get();
    fap.getProjection().ifPresent(pj -> {
      pipeline.add(Aggregates.project(finalProjection.map(fp -> Projections.fields(fp, pj)).orElse(pj)));
    });
    pipeline.add(Aggregates.match(fap.getMatch()));
  } else {
    pipeline.add(Aggregates.match(filter.getExpression()));
  }
  return pipeline;
}

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

/**
 * 统计频数
 *
 * @param collectionName
 * @param match
 * @param distinctField
 * @return
 */
public int distinctCount(String collectionName, Document match, String distinctField) {
  AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
      Arrays.asList(
          match(match)
          , group(null, addToSet("_array", "$" + distinctField))
          , project(new Document("_num", new Document("$size", "$_array")))
      )
  );
  Document first = aggregate.first();
  if (first != null) {
    return first.getInteger("_num");
  }
  return 0;
}

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

@Override
public QueryResult<Group> getGroup(long studyId, @Nullable String groupId, List<String> userIds) throws CatalogDBException {
  long startTime = startQuery();
  checkId(studyId);
  if (userIds == null) {
    userIds = Collections.emptyList();
  }
  List<Bson> aggregation = new ArrayList<>();
  aggregation.add(Aggregates.match(Filters.eq(PRIVATE_UID, studyId)));
  aggregation.add(Aggregates.project(Projections.include(QueryParams.GROUPS.key())));
  aggregation.add(Aggregates.unwind("$" + QueryParams.GROUPS.key()));
  if (userIds.size() > 0) {
    aggregation.add(Aggregates.match(Filters.in(QueryParams.GROUP_USER_IDS.key(), userIds)));
  }
  if (groupId != null && groupId.length() > 0) {
    aggregation.add(Aggregates.match(Filters.eq(QueryParams.GROUP_NAME.key(), groupId)));
  }
  QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, null);
  List<Study> studies = MongoDBUtils.parseStudies(queryResult);
  List<Group> groups = new ArrayList<>();
  studies.stream().filter(study -> study.getGroups() != null).forEach(study -> groups.addAll(study.getGroups()));
  return endQuery("getGroup", startTime, groups);
}

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

@Override
public Long variableSetExists(long variableSetId) {
  List<Bson> aggregation = new ArrayList<>();
  aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(),
      Filters.eq(VariableSetParams.UID.key(), variableSetId))));
  aggregation.add(Aggregates.project(Projections.include(QueryParams.VARIABLE_SET.key())));
  aggregation.add(Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key()));
  aggregation.add(Aggregates.match(Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId)));
  QueryResult<VariableSet> queryResult = studyCollection.aggregate(aggregation, variableSetConverter, new QueryOptions());
  return (long) queryResult.getResult().size();
}

代码示例来源:origin: ozlerhakan/mongolastic

/**
   * Get the MongoDB cursor.
   */
  private MongoCursor<Document> getCursor(int skip) {
    if (cursor == null && cursorId == 0) {
      Document query = Document.parse(config.getMongo().getQuery());
      List<Bson> pipes = new ArrayList<>(3);
      pipes.add(match(query));
      pipes.add(skip(skip));

      Optional.ofNullable(config.getMongo().getProject()).ifPresent(p -> pipes.add(project(Document.parse(p))));

      AggregateIterable<Document> aggregate = collection.aggregate(pipes)
          .allowDiskUse(true)
          .useCursor(true);

      cursor = aggregate.iterator();

      // TODO: Persist cursor ID somewhere to allow restarts.
      Optional.ofNullable(cursor.getServerCursor()).ifPresent(serverCursor -> cursorId = serverCursor.getId());
    } else if (cursor == null && cursorId != 0) {
      // TODO: Lookup cursor ID for resume.
      // Open existing cursor in case of restart??
    }

    return cursor;
  }
}

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

@Override
public QueryResult nativeGet(Query query, QueryOptions options) {
  Bson bson = parseQuery(query);
  Bson match = Aggregates.match(bson);
  Bson project = Aggregates.project(Projections.include("transcripts.xrefs"));
  Bson unwind = Aggregates.unwind("$transcripts");
  Bson unwind2 = Aggregates.unwind("$transcripts.xrefs");
  // This project the three fields of Xref to the top of the object
  Document document = new Document("id", "$transcripts.xrefs.id");
  document.put("dbName", "$transcripts.xrefs.dbName");
  document.put("dbDisplayName", "$transcripts.xrefs.dbDisplayName");
  Bson project1 = Aggregates.project(document);
  if (query.containsKey(QueryParams.DBNAME.key())) {
    Bson bson2 = parseQuery(new Query(QueryParams.DBNAME.key(), query.get(QueryParams.DBNAME.key())));
    Bson match2 = Aggregates.match(bson2);
    return mongoDBCollection.aggregate(Arrays.asList(match, project, unwind, unwind2, match2, project1), options);
  }
  return mongoDBCollection.aggregate(Arrays.asList(match, project, unwind, unwind2, project1), options);
}

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

@Override
public QueryResult nativeGet(Query query, QueryOptions options) {
  Bson bson = parseQuery(query);
  Bson match = Aggregates.match(bson);
  Bson project = Aggregates.project(Projections.include("transcripts.xrefs"));
  Bson unwind = Aggregates.unwind("$transcripts");
  Bson unwind2 = Aggregates.unwind("$transcripts.xrefs");
  // This project the three fields of Xref to the top of the object
  Document document = new Document("id", "$transcripts.xrefs.id");
  document.put("dbName", "$transcripts.xrefs.dbName");
  document.put("dbDisplayName", "$transcripts.xrefs.dbDisplayName");
  Bson project1 = Aggregates.project(document);
  if (query.containsKey(QueryParams.DBNAME.key())) {
    Bson bson2 = parseQuery(new Query(QueryParams.DBNAME.key(), query.get(QueryParams.DBNAME.key())));
    Bson match2 = Aggregates.match(bson2);
    return mongoDBCollection.aggregate(Arrays.asList(match, project, unwind, unwind2, match2, project1), options);
  }
  return mongoDBCollection.aggregate(Arrays.asList(match, project, unwind, unwind2, project1), options);
}

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

private static Bson createGroupedIdProjectStage() {
  return project(new BsonDocument()
      .append(FIELD_ID, new BsonString(ID_VARIABLE + "." + FIELD_ID))
      .append(FIELD_NAMESPACE, BsonBoolean.TRUE)
      .append(FIELD_ATTRIBUTES, BsonBoolean.TRUE)
      .append(FIELD_FEATURES, BsonBoolean.TRUE)
      .append(FIELD_INTERNAL, BsonBoolean.TRUE)
      .append(FIELD_DELETED, BsonBoolean.TRUE)
      .append(FIELD_REVISION, BsonBoolean.TRUE));
}

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

private static Bson createGroupedIdProjectStage() {
  return project(new BsonDocument()
      .append(FIELD_ID, new BsonString(ID_VARIABLE + "." + FIELD_ID))
      .append(FIELD_NAMESPACE, BsonBoolean.TRUE)
      .append(FIELD_ATTRIBUTES, BsonBoolean.TRUE)
      .append(FIELD_FEATURES, BsonBoolean.TRUE)
      .append(FIELD_INTERNAL, BsonBoolean.TRUE)
      .append(FIELD_DELETED, BsonBoolean.TRUE)
      .append(FIELD_DELETED_FLAG, BsonBoolean.TRUE)
      .append(FIELD_REVISION, BsonBoolean.TRUE));
}

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

@Override
public void saveAnnotation(String name, ObjectMap options) throws StorageEngineException, VariantAnnotatorException {
  dbAdaptor.getStudyConfigurationManager().lockAndUpdateProject(project -> {
    registerNewAnnotationSnapshot(name, variantAnnotator, project);
    return project;
  });
  String annotationCollectionName = mongoDbAdaptor.getAnnotationCollectionName(name);
  mongoDbAdaptor.getVariantsCollection()
      .nativeQuery()
      .aggregate(Arrays.asList(
          match(exists(ANNOTATION_FIELD + '.' + ANNOT_ID_FIELD)),
          project(include(
              CHROMOSOME_FIELD,
              START_FIELD,
              REFERENCE_FIELD,
              ALTERNATE_FIELD,
              END_FIELD,
              TYPE_FIELD,
              SV_FIELD,
              RELEASE_FIELD,
              ANNOTATION_FIELD,
              CUSTOM_ANNOTATION_FIELD)),
          out(annotationCollectionName)))
      .toCollection();
}

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

private static Bson createFirstProjectionStage() {
  final BsonDocument projection = new BsonDocument()
      .append(POLICY_INDEX_ID, new BsonDocument(CONCAT,
          new BsonArray(Arrays.asList(
              new BsonString(ID_VARIABLE),
              new BsonString(":"),
              new BsonDocument(IF_NULL_CONDITION,
                  new BsonArray(Arrays.asList(
                      new BsonString(FIELD_INTERNAL_FEATURE_VARIABLE),
                      new BsonString(""))
                  )),
              new BsonDocument(IF_NULL_CONDITION,
                  new BsonArray(Arrays.asList(
                      new BsonString(FIELD_INTERNAL_KEY_VARIABLE),
                      new BsonString(""))
                  ))
          ))
      ))
      .append(FIELD_NAMESPACE, BsonBoolean.TRUE)
      .append(FIELD_ATTRIBUTES, BsonBoolean.TRUE)
      .append(FIELD_FEATURES, BsonBoolean.TRUE)
      .append(FIELD_INTERNAL, BsonBoolean.TRUE)
      .append(FIELD_DELETED, BsonBoolean.TRUE)
      .append(FIELD_REVISION, BsonBoolean.TRUE);
  return project(projection);
}

相关文章