List of usage examples for com.mongodb.client.model Projections include
public static Bson include(final List<String> fieldNames)
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
/** * The method will return the variable object given variableSetId and the variableId. * @param variableSetId Id of the variableSet. * @param variableId Id of the variable inside the variableSet. * @return the variable object.//w w w . ja v a 2 s.c o m */ private Variable getVariable(long variableSetId, String variableId) throws CatalogDBException { List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(), Filters.eq(VariableSetParams.ID.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_ID.key(), variableSetId))); aggregation.add( Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE.key())); aggregation.add(Aggregates.match(Filters .eq(QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE_NAME.key(), variableId))); QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, new QueryOptions()); Document variableSetDocument = (Document) queryResult.first().get(QueryParams.VARIABLE_SET.key()); VariableSet variableSet = variableSetConverter.convertToDataModelType(variableSetDocument); Iterator<Variable> iterator = variableSet.getVariables().iterator(); if (iterator.hasNext()) { return iterator.next(); } else { // This error should never be raised. throw new CatalogDBException( "VariableSet {id: " + variableSetId + "} - Could not obtain variable object."); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> getAllVariableSets(long studyId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Bson> mongoQueryList = new LinkedList<>(); for (Map.Entry<String, Object> entry : options.entrySet()) { String key = entry.getKey().split("\\.")[0]; try {/*from ww w . j a va 2 s . c o m*/ if (isDataStoreOption(key) || isOtherKnownOption(key)) { continue; //Exclude DataStore options } CatalogStudyDBAdaptor.VariableSetParams option = CatalogStudyDBAdaptor.VariableSetParams .getParam(key) != null ? CatalogStudyDBAdaptor.VariableSetParams.getParam(key) : CatalogStudyDBAdaptor.VariableSetParams.getParam(entry.getKey()); switch (option) { case STUDY_ID: addCompQueryFilter(option, option.name(), PRIVATE_ID, options, mongoQueryList); break; default: String optionsKey = "variableSets." + entry.getKey().replaceFirst(option.name(), option.key()); addCompQueryFilter(option, entry.getKey(), optionsKey, options, mongoQueryList); break; } } catch (IllegalArgumentException e) { throw new CatalogDBException(e); } } /* QueryResult<DBObject> queryResult = studyCollection.aggregate(Arrays.<DBObject>asList( new BasicDBObject("$match", new BasicDBObject(PRIVATE_ID, studyId)), new BasicDBObject("$project", new BasicDBObject("variableSets", 1)), new BasicDBObject("$unwind", "$variableSets"), new BasicDBObject("$match", new BasicDBObject("$and", mongoQueryList)) ), filterOptions(options, FILTER_ROUTE_STUDIES)); */ List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, studyId))); aggregation.add(Aggregates.project(Projections.include("variableSets"))); aggregation.add(Aggregates.unwind("$variableSets")); if (mongoQueryList.size() > 0) { aggregation.add(Aggregates.match(Filters.and(mongoQueryList))); } QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, filterOptions(options, FILTER_ROUTE_STUDIES)); List<VariableSet> variableSets = parseObjects(queryResult, Study.class).stream() .map(study -> study.getVariableSets().get(0)).collect(Collectors.toList()); return endQuery("", startTime, variableSets); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public long getStudyIdByVariableSetId(long variableSetId) throws CatalogDBException { // DBObject query = new BasicDBObject("variableSets.id", variableSetId); Bson query = Filters.eq("variableSets.id", variableSetId); Bson projection = Projections.include("id"); // QueryResult<DBObject> queryResult = studyCollection.find(query, new BasicDBObject("id", true), null); QueryResult<Document> queryResult = studyCollection.find(query, projection, null); if (!queryResult.getResult().isEmpty()) { Object id = queryResult.getResult().get(0).get("id"); return id instanceof Number ? ((Number) id).intValue() : (int) Double.parseDouble(id.toString()); } else {//from w w w. j a v a2s .c o m throw CatalogDBException.idNotFound("VariableSet", variableSetId); } }
From source file:org.opencb.opencga.catalog.db.mongodb.MongoDBAdaptor.java
License:Apache License
protected QueryResult rank(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, int numResults, boolean asc) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); }// w w w .ja v a 2 s . c o m 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()); } }
From source file:org.opencb.opencga.catalog.db.mongodb.MongoDBAdaptor.java
License:Apache License
protected QueryResult groupBy(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, QueryOptions options) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); }//from w ww. j av a 2 s . c om List<String> groupByFields = new ArrayList<>(groupByField); // if (groupByField.size() == 1) { // // if only one field then we call to simple groupBy // return groupBy(collection, query, groupByField.get(0), idField, options); // } else { Bson match = Aggregates.match(query); // add all group-by fields to the projection together with the aggregation field name List<String> includeGroupByFields = new ArrayList<>(groupByField); includeGroupByFields.add(idField); List<Bson> projections = new ArrayList<>(); addDateProjection(projections, includeGroupByFields, groupByFields); projections.add(Projections.include(includeGroupByFields)); Bson project = Aggregates.project(Projections.fields(projections)); // Bson project = Aggregates.project(Projections.include(groupByFields)); // _id document creation to have the multiple id Document id = new Document(); for (String s : groupByFields) { id.append(s, "$" + s); } Bson group; if (options.getBoolean("count", false)) { group = Aggregates.group(id, Accumulators.sum("count", 1)); } else { group = Aggregates.group(id, Accumulators.addToSet("features", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); // } }
From source file:org.opencb.opencga.catalog.db.mongodb.ProjectMongoDBAdaptor.java
License:Apache License
@Override public long getId(String userId, String projectAlias) throws CatalogDBException { QueryResult<Document> queryResult = userCollection.find( new BsonDocument("projects.alias", new BsonString(projectAlias)).append("id", new BsonString(userId)), Projections.fields(Projections.include("projects.id"), Projections.elemMatch("projects", Filters.eq("alias", projectAlias))), null);//from www. ja v a2s . c o m /* QueryResult<DBObject> queryResult = userCollection.find( BasicDBObjectBuilder .start("projects.alias", projectAlias) .append("id", userId).get(), BasicDBObjectBuilder.start("projects.id", true) .append("projects", new BasicDBObject("$elemMatch", new BasicDBObject("alias", projectAlias))).get(), null );*/ User user = parseUser(queryResult); if (user == null || user.getProjects().isEmpty()) { return -1; } else { return user.getProjects().get(0).getId(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.ProjectMongoDBAdaptor.java
License:Apache License
@Override public String getOwnerId(long projectId) throws CatalogDBException { // DBObject query = new BasicDBObject("projects.id", projectId); Bson query = Filters.eq("projects.id", projectId); // DBObject projection = new BasicDBObject("id", "true"); Bson projection = Projections.include("id"); // QueryResult<DBObject> result = userCollection.find(query, projection, null); QueryResult<Document> result = userCollection.find(query, projection, null); if (result.getResult().isEmpty()) { throw CatalogDBException.idNotFound("Project", projectId); } else {//from w w w . jav a 2 s. c om return result.getResult().get(0).get("id").toString(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.ProjectMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Project> get(Query query, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); if (!query.containsKey(QueryParams.STATUS_NAME.key())) { query.append(QueryParams.STATUS_NAME.key(), "!=" + Status.TRASHED + ";!=" + Status.DELETED); }//from w w w . ja v a 2 s.c om List<Bson> aggregates = new ArrayList<>(); aggregates.add(Aggregates.unwind("$projects")); aggregates.add(Aggregates.match(parseQuery(query))); // Check include if (options != null && options.get(QueryOptions.INCLUDE) != null) { List<String> includeList = new ArrayList<>(); List<String> optionsAsStringList = options.getAsStringList(QueryOptions.INCLUDE); includeList.addAll(optionsAsStringList.stream().collect(Collectors.toList())); if (!includeList.contains(QueryParams.ID.key())) { includeList.add(QueryParams.ID.key()); } // Check if they start with projects. for (int i = 0; i < includeList.size(); i++) { if (!includeList.get(i).startsWith("projects.")) { String param = "projects." + includeList.get(i); includeList.set(i, param); } } if (includeList.size() > 0) { aggregates.add(Aggregates.project(Projections.include(includeList))); } } QueryResult<Project> projectQueryResult = userCollection.aggregate(aggregates, projectConverter, options); if (options == null || !options.containsKey(QueryOptions.EXCLUDE) || !options.getAsStringList(QueryOptions.EXCLUDE).contains("projects.studies")) { for (Project project : projectQueryResult.getResult()) { Query studyQuery = new Query(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), project.getId()); try { QueryResult<Study> studyQueryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor() .get(studyQuery, options); project.setStudies(studyQueryResult.getResult()); } catch (CatalogDBException e) { e.printStackTrace(); } } } return endQuery("Get project", startTime, projectQueryResult.getResult()); }
From source file:org.opencb.opencga.catalog.db.mongodb.ProjectMongoDBAdaptor.java
License:Apache License
@Override public QueryResult nativeGet(Query query, QueryOptions options) throws CatalogDBException { if (!query.containsKey(QueryParams.STATUS_NAME.key())) { query.append(QueryParams.STATUS_NAME.key(), "!=" + Status.TRASHED + ";!=" + Status.DELETED); }//from w ww. j a v a 2 s . c om List<Bson> aggregates = new ArrayList<>(); aggregates.add(Aggregates.match(parseQuery(query))); aggregates.add(Aggregates.unwind("$projects")); // Check include & excludes if (options != null && options.get(QueryOptions.INCLUDE) != null) { List<String> includeList = new ArrayList<>(); List<String> optionsAsStringList = options.getAsStringList(QueryOptions.INCLUDE); includeList.addAll(optionsAsStringList.stream().collect(Collectors.toList())); if (includeList.size() > 0) { aggregates.add(Aggregates.project(Projections.include(includeList))); } } QueryResult<Document> projectQueryResult = userCollection.aggregate(aggregates, options); ArrayList<Document> returnedProjectList = new ArrayList<>(); for (Document user : projectQueryResult.getResult()) { Document project = (Document) user.get("projects"); Query studyQuery = new Query(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), project.get("id")); QueryResult studyQueryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(studyQuery, options); project.remove("studies"); project.append("studies", studyQueryResult.getResult()); returnedProjectList.add(project); } projectQueryResult.setResult(returnedProjectList); return projectQueryResult; }
From source file:org.opencb.opencga.catalog.db.mongodb.SampleMongoDBAdaptor.java
License:Apache License
@Override public long getStudyId(long sampleId) throws CatalogDBException { /*DBObject query = new BasicDBObject(PRIVATE_ID, sampleId); BasicDBObject projection = new BasicDBObject(PRIVATE_STUDY_ID, true); QueryResult<DBObject> queryResult = sampleCollection.find(query, projection, null);*/ Bson query = new Document(PRIVATE_ID, sampleId); Bson projection = Projections.include(PRIVATE_STUDY_ID); QueryResult<Document> queryResult = sampleCollection.find(query, projection, null); if (!queryResult.getResult().isEmpty()) { Object studyId = queryResult.getResult().get(0).get(PRIVATE_STUDY_ID); return studyId instanceof Number ? ((Number) studyId).longValue() : Long.parseLong(studyId.toString()); } else {/*from www .j a v a 2 s.com*/ throw CatalogDBException.idNotFound("Sample", sampleId); } }