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.StudyMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Group> getGroup(long studyId, @Nullable String groupId, List<String> userIds) throws CatalogDBException { long startTime = startQuery(); checkId(studyId);/*from w w w .ja v a 2s .c om*/ for (String userId : userIds) { dbAdaptorFactory.getCatalogUserDBAdaptor().checkId(userId); } if (groupId != null && groupId.length() > 0 && !groupExists(studyId, groupId)) { throw new CatalogDBException("Group \"" + groupId + "\" does not exist in study " + studyId); } /* * 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))); QueryResult<VariableSet> queryResult = studyCollection.aggregate(aggregation, variableSetConverter, new QueryOptions()); * */ List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, studyId))); aggregation.add(Aggregates.project(Projections.include(QueryParams.GROUPS.key()))); aggregation.add(Aggregates.unwind("$" + QueryParams.GROUPS.key())); // Document query = new Document(PRIVATE_ID, studyId); // List<Bson> groupQuery = new ArrayList<>(); if (userIds.size() > 0) { aggregation.add(Aggregates.match(Filters.in(QueryParams.GROUP_USER_IDS.key(), userIds))); // groupQuery.add(Filters.in("userIds", userIds)); } if (groupId != null && groupId.length() > 0) { aggregation.add(Aggregates.match(Filters.eq(QueryParams.GROUP_NAME.key(), groupId))); // groupQuery.add(Filters.eq("id", groupId)); } // Bson projection = new Document(QueryParams.GROUPS.key(), new Document("$elemMatch", groupQuery)); QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, null); // QueryResult<Document> queryResult = studyCollection.find(query, projection, 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); }
From source file:org.opencb.opencga.catalog.db.mongodb.StudyMongoDBAdaptor.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 w ww .ja v a 2s . c o m if (isDataStoreOption(key) || isOtherKnownOption(key)) { continue; //Exclude DataStore options } StudyDBAdaptor.VariableSetParams option = StudyDBAdaptor.VariableSetParams.getParam(key) != null ? StudyDBAdaptor.VariableSetParams.getParam(key) : StudyDBAdaptor.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.UserMongoDBAdaptor.java
License:Apache License
@Override public String getUserIdBySessionId(String sessionId) { Bson query = new Document("sessions", new Document("$elemMatch", new Document("id", sessionId).append("logout", ""))); Bson projection = Projections.include(QueryParams.ID.key()); QueryResult<Document> id = userCollection.find(query, projection, null); if (id.getNumResults() != 0) { return (String) (id.getResult().get(0)).get("id"); } else {//from w ww . j av a2s . c o m return ""; } }
From source file:rapture.table.mongodb.MongoIndexHandler.java
License:Open Source License
@Override public List<TableRecord> queryTable(final TableQuery querySpec) { MongoRetryWrapper<List<TableRecord>> wrapper = new MongoRetryWrapper<List<TableRecord>>() { @Override//from w w w.j a v a 2 s . c om public FindIterable<Document> makeCursor() { FindIterable<Document> ret; MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName); // Convert the query into that for a mongodb collection, then // call find Document query = EpochManager.getNotEqualEpochQueryObject(); if (querySpec.getFieldTests() != null) { for (TableSelect sel : querySpec.getFieldTests()) { switch (sel.getOper()) { case "=": query.append(sel.getFieldName(), sel.getTestValue()); break; case ">": Document gt = new Document(); gt.append("$gt", sel.getTestValue()); query.append(sel.getFieldName(), gt); break; case "<": Document lt = new Document(); lt.append("$lt", sel.getTestValue()); query.append(sel.getFieldName(), lt); break; case "LIKE": query.append(sel.getFieldName(), java.util.regex.Pattern.compile(sel.getTestValue().toString())); break; } } } // That's the query, now determine the return fields... List<String> projection = querySpec.getFieldReturns(); if (projection != null) { projection.add(KEY); } // Now we need to do the query, with a limit and skip applied if // necessary Document sort = new Document(); if (querySpec.getSortFields() != null) { for (TableColumnSort sortField : querySpec.getSortFields()) { sort.put(sortField.getFieldName(), sortField.getAscending() ? 1 : -1); } } ret = collection.find(query).projection(Projections .include((List<String>) ((projection == null) ? Collections.emptyList() : projection))); if (!sort.isEmpty()) { ret = ret.sort(sort); } if (querySpec.getSkip() != 0) { ret = ret.skip(querySpec.getSkip()); } if (querySpec.getLimit() != 0) { ret = ret.limit(querySpec.getLimit()); } return ret; } @Override public List<TableRecord> action(FindIterable<Document> cursor) { List<TableRecord> records = new ArrayList<>(); if (cursor != null) { for (Document obj : cursor) { if (obj != null) { TableRecord rec = new TableRecord(); rec.setKeyName(obj.getString(KEY)); rec.setFields(obj); rec.setContent(obj.toString()); records.add(rec); } } } return records; } }; return wrapper.doAction(); }