/** * Creates a projection that includes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson include(final String... fieldNames) { return include(asList(fieldNames)); }
/** * Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes * precedence. * * @param projections the list of projections to combine * @return the combined projection */ public static Bson fields(final Bson... projections) { return fields(asList(projections)); }
/** * Creates a projection that excludes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson exclude(final String... fieldNames) { return exclude(asList(fieldNames)); }
List<String> includeStringList = options.getAsStringList(QueryOptions.INCLUDE, ","); if (includeStringList != null && includeStringList.size() > 0) { include = Projections.include(includeStringList); List<String> excludeStringList = options.getAsStringList(QueryOptions.EXCLUDE, ","); if (excludeStringList != null && excludeStringList.size() > 0) { exclude = Projections.exclude(excludeStringList); excludeId = excludeStringList.contains("_id"); projections.add(Projections.excludeId()); projectionResult = Projections.fields(projections);
@Override public Set<String> getArtifactsIds(DBKey dbKey) { final String dbName = MongoDBClient.getDbName(dbKey.getCompany(), dbKey.getProject()); FindIterable<Document> findIterable = client.getDatabase(dbName) .getCollection(ARTIFACTS_COLLECTION_NAME + FILES_COLLECTION_SUFFIX) .find().projection(fields(include(ID_FIELD_NAME))); Set<String> artifactsId = new HashSet<>(); for (Document document : findIterable) { artifactsId.add(document.get(ID_FIELD_NAME).toString()); } return artifactsId; }
@Override public QueryResult<String> getCdna(String id) { Bson bson = Filters.eq("transcripts.xrefs.id", id); Bson elemMatch = Projections.elemMatch("transcripts", Filters.eq("xrefs.id", id)); Bson include = Projections.include("transcripts.cDnaSequence"); // elemMatch and include are combined to reduce the data sent from the server Bson projection = Projections.fields(elemMatch, include); QueryResult<Document> result = mongoDBCollection.find(bson, projection, new QueryOptions()); String sequence = null; if (result != null && !result.getResult().isEmpty()) { List<Document> transcripts = (List<Document>) result.getResult().get(0).get("transcripts"); sequence = transcripts.get(0).getString("cDnaSequence"); } return new QueryResult<>(id, result.getDbTime(), result.getNumResults(), result.getNumTotalResults(), result.getWarningMsg(), result.getErrorMsg(), Collections.singletonList(sequence)); }
/** * Finds and returns the of computational resource. * * @param user user name. * @param exploratoryName the name of exploratory. * @param computationalName name of computational resource. * @throws DlabException if exception occurs */ public UserComputationalResource fetchComputationalFields(String user, String exploratoryName, String computationalName) { Optional<UserInstanceDTO> opt = findOne(USER_INSTANCES, and(exploratoryCondition(user, exploratoryName), elemMatch(COMPUTATIONAL_RESOURCES, eq(COMPUTATIONAL_NAME, computationalName))), fields(include(COMPUTATIONAL_RESOURCES), excludeId()), UserInstanceDTO.class); if (opt.isPresent()) { List<UserComputationalResource> list = opt.get().getResources(); UserComputationalResource comp = list.stream() .filter(r -> r.getComputationalName().equals(computationalName)) .findFirst() .orElse(null); if (comp != null) { return comp; } } throw new DlabException("Computational resource " + computationalName + " for user " + user + " with " + "exploratory name " + exploratoryName + " not found."); }
/** * Finds and returns the list of user resources. * * @param user name * @return list of user resources */ public Iterable<Document> findExploratory(String user) { return find(USER_INSTANCES, eq(USER, user), fields(exclude(ExploratoryLibDAO.EXPLORATORY_LIBS, ExploratoryLibDAO.COMPUTATIONAL_LIBS, SCHEDULER_DATA))); }
fields.add(Projections.excludeId()); fields.add(Projections.computed(VALUES, values)); fields.add(Projections.computed(HASHES, hashes)); if (!types.isEmpty()) { fields.add(Projections.computed(TYPES, types)); fields.add(Projections.computed(LEVEL, new Document("$max", Arrays.asList("$" + LEVEL, getFieldExpr.apply(LEVEL), 0)))); fields.add(Projections.computed(TIMESTAMP, new Document("$max", Arrays.asList("$" + TIMESTAMP, getFieldExpr.apply(TIMESTAMP), 0)))); return Projections.fields(fields);
Document doc = findOne(USER_INSTANCES, exploratoryCondition(user, exploratoryName), and(elemMatch(COMPUTATIONAL_RESOURCES, and(eq(ComputationalDAO.COMPUTATIONAL_NAME, computationalName), eq(COMPUTATIONAL_SPOT, true), not(eq(STATUS, TERMINATED.toString())))), include(COMPUTATIONAL_RESOURCES + "." + COMPUTATIONAL_SPOT)) ).orElse(null); if (doc == null || doc.get(COMPUTATIONAL_RESOURCES) == null) {
public MongoAggregation excludeId() { projections.add(Projections.excludeId()); return this; }
/** * Return condition for search library into exploratory data. * * @param libraryGroup the name of group. * @param libraryName the name of library. * @param libraryVersion the name of library. */ private static Bson libraryConditionExploratory(String libraryGroup, String libraryName, String libraryVersion) { return elemMatch(EXPLORATORY_LIBS, libCondition(libraryGroup, libraryName, libraryVersion)); }
List<String> stringList = queryOptions.getAsStringList("exclude"); if (stringList.contains("_id")) { exclude = Aggregates.project(Projections.exclude("_id")); includeAndExclude = Aggregates.project(Projections.fields(Projections.excludeId(), Projections.include("transcripts.tfbs"))); } else { includeAndExclude = Aggregates.project(Projections.include("transcripts.tfbs"));
@Override public Set<String> getArtifactsIds(DBKey dbKey) { final String dbName = MongoDBClient.getDbName(dbKey.getCompany(), dbKey.getProject()); FindIterable<Document> findIterable = client.getDatabase(dbName) .getCollection(ARTIFACTS_COLLECTION_NAME + FILES_COLLECTION_SUFFIX) .find().projection(fields(include(ID_FIELD_NAME))); Set<String> artifactsId = new HashSet<>(); for (Document document : findIterable) { artifactsId.add(document.get(ID_FIELD_NAME).toString()); } return artifactsId; }
@Override public QueryResult<String> getCdna(String id) { Bson bson = Filters.eq("transcripts.xrefs.id", id); Bson elemMatch = Projections.elemMatch("transcripts", Filters.eq("xrefs.id", id)); Bson include = Projections.include("transcripts.cDnaSequence"); // elemMatch and include are combined to reduce the data sent from the server Bson projection = Projections.fields(elemMatch, include); QueryResult<Document> result = mongoDBCollection.find(bson, projection, new QueryOptions()); String sequence = null; if (result != null && !result.getResult().isEmpty()) { List<Document> transcripts = (List<Document>) result.getResult().get(0).get("transcripts"); sequence = transcripts.get(0).getString("cDnaSequence"); } return new QueryResult<>(id, result.getDbTime(), result.getNumResults(), result.getNumTotalResults(), result.getWarningMsg(), result.getErrorMsg(), Collections.singletonList(sequence)); }