List of usage examples for com.mongodb.client.model Filters eq
public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value)
From source file:org.opencb.opencga.catalog.db.mongodb.SampleMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Sample> insert(Sample sample, long studyId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); dbAdaptorFactory.getCatalogStudyDBAdaptor().checkId(studyId); /*//w w w. jav a 2s. co m QueryResult<Long> count = sampleCollection.count( new BasicDBObject("name", sample.getName()).append(PRIVATE_STUDY_ID, studyId)); */ Bson bson = Filters.and(Filters.eq("name", sample.getName()), Filters.eq(PRIVATE_STUDY_ID, studyId)); QueryResult<Long> count = sampleCollection.count(bson); // new BsonDocument("name", new BsonString(sample.getName())).append(PRIVATE_STUDY_ID, new BsonInt32(studyId))); if (count.getResult().get(0) > 0) { throw new CatalogDBException("Sample { name: '" + sample.getName() + "'} already exists."); } long sampleId = getNewId(); sample.setId(sampleId); sample.setAnnotationSets(Collections.<AnnotationSet>emptyList()); //TODO: Add annotationSets Document sampleObject = sampleConverter.convertToStorageType(sample); sampleObject.put(PRIVATE_STUDY_ID, studyId); sampleObject.put(PRIVATE_ID, sampleId); sampleCollection.insert(sampleObject, null); return endQuery("createSample", startTime, get(sampleId, options)); }
From source file:org.opencb.opencga.catalog.db.mongodb.SampleMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Sample> update(long sampleId, QueryOptions parameters) throws CatalogDBException { long startTime = startQuery(); Map<String, Object> sampleParams = new HashMap<>(); //List<Bson> sampleParams = new ArrayList<>(); String[] acceptedParams = { "source", "description", "name" }; filterStringParams(parameters, sampleParams, acceptedParams); if (sampleParams.containsKey("name")) { // Check that the new sample name is still unique long studyId = getStudyId(sampleId); QueryResult<Long> count = sampleCollection .count(new Document("name", sampleParams.get("name")).append(PRIVATE_STUDY_ID, studyId)); if (count.getResult().get(0) > 0) { throw new CatalogDBException("Sample { name: '" + sampleParams.get("name") + "'} already exists."); }//from w w w.ja v a 2s. c o m } String[] acceptedIntParams = { "individualId" }; filterIntParams(parameters, sampleParams, acceptedIntParams); String[] acceptedMapParams = { "attributes" }; filterMapParams(parameters, sampleParams, acceptedMapParams); if (sampleParams.containsKey("individualId")) { int individualId = parameters.getInt("individualId"); if (individualId > 0 && !dbAdaptorFactory.getCatalogIndividualDBAdaptor().exists(individualId)) { throw CatalogDBException.idNotFound("Individual", individualId); } } if (!sampleParams.isEmpty()) { /*QueryResult<WriteResult> update = sampleCollection.update(new BasicDBObject(PRIVATE_ID, sampleId), new BasicDBObject("$set", sampleParams), null); */ Bson query = Filters.eq(PRIVATE_ID, sampleId); Bson operation = new Document("$set", sampleParams); QueryResult<UpdateResult> update = sampleCollection.update(query, operation, null); if (update.getResult().isEmpty() || update.getResult().get(0).getModifiedCount() == 0) { throw CatalogDBException.idNotFound("Sample", sampleId); } } return endQuery("Modify sample", startTime, get(sampleId, parameters)); }
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);/* ww w .jav a 2 s . c o m*/ 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 void removeUsersFromGroup(long studyId, String groupId, List<String> members) throws CatalogDBException { for (String member : members) { dbAdaptorFactory.getCatalogUserDBAdaptor().checkId(member); }/*www . ja v a 2s .c o m*/ Bson and = Filters.and(Filters.eq(PRIVATE_ID, studyId), Filters.eq("groups.name", groupId)); Bson pull = Updates.pullAll("groups.$.userIds", members); QueryResult<UpdateResult> update = studyCollection.update(and, pull, null); if (update.first().getModifiedCount() != 1) { throw new CatalogDBException("Unable to remove members from group " + groupId); } }
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 . j av a 2 s.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 QueryResult<Session> getSession(String userId, String sessionId) throws CatalogDBException { long startTime = startQuery(); // BasicDBObject query = new BasicDBObject("id", userId); // query.put("sessions.id", sessionId); Query query1 = new Query(QueryParams.ID.key(), userId).append(QueryParams.SESSION_ID.key(), sessionId); Bson bson = parseQuery(query1);/* w w w. jav a 2s. c o m*/ // BasicDBObject projection = new BasicDBObject("sessions", // new BasicDBObject("$elemMatch", // new BasicDBObject("id", sessionId))); Bson projection = Projections.elemMatch("sessions", Filters.eq("id", sessionId)); // QueryResult<DBObject> result = userCollection.find(query, projection, null); QueryResult<Document> documentQueryResult = userCollection.find(bson, projection, null); User user = parseUser(documentQueryResult); if (user != null) { return endQuery("getSession", startTime, user.getSessions()); } return endQuery("getSession", startTime, Arrays.asList()); }
From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java
License:Apache License
@Override public void addQueryFilter(String userId, QueryFilter queryFilter) throws CatalogDBException { // Check if there exists a filter for that user with the same id Bson checkFilterExists = Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId())); QueryResult<Long> count = userCollection.count(checkFilterExists); if (count.getResult().get(0) != 0) { throw new CatalogDBException( "There already exists a filter with name " + queryFilter.getId() + " for user " + userId); }//from w ww. j ava 2 s . c om // Insert the filter Bson query = Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.ne(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId())); Bson filterDocument = getMongoDBDocument(queryFilter, "Filter"); Bson update = Updates.push(QueryParams.CONFIG_OPENCGA_FILTERS.key(), filterDocument); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null); if (queryResult.first().getModifiedCount() != 1) { if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException( "User: There was an error when trying to store the filter. It could not be stored."); } else { // This error should NEVER be raised. throw new CatalogDBException( "User: There was a critical error when storing the filter. Is has been inserted " + queryResult.first().getModifiedCount() + " times."); } } }
From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Long> deleteQueryFilter(String userId, String filterId) throws CatalogDBException { long startTime = startQuery(); // Delete the filter from the database Bson query = Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", filterId)); Bson update = Updates.pull(QueryParams.CONFIG_OPENCGA_FILTERS.key(), Filters.eq("id", filterId)); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null); return endQuery("Delete query filter", startTime, Collections.singletonList(queryResult.first().getModifiedCount())); }
From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<QueryFilter> getQueryFilter(String userId, String filterId) throws CatalogDBException { List<Bson> aggregates = new ArrayList<>(); aggregates.add(Aggregates.unwind("$" + QueryParams.CONFIG_OPENCGA_FILTERS.key())); aggregates.add(Aggregates.match(Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", filterId)))); QueryResult<QueryFilter> aggregate = userCollection.aggregate(aggregates, filterConverter, new QueryOptions()); if (aggregate.getNumResults() == 0) { throw new CatalogDBException("The filter " + filterId + " could not be found in the database."); }/*from w w w. j av a 2s .c o m*/ return aggregate; }
From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantSourceMongoDBAdaptor.java
License:Apache License
@Override public void updateVariantSource(VariantSource variantSource) { MongoDBCollection coll = db.getCollection(collectionName); Document document = variantSourceConverter.convertToStorageType(variantSource); String id = document.getString("_id"); document.append("_id", id); QueryOptions options = new QueryOptions(MongoDBCollection.REPLACE, true).append(MongoDBCollection.UPSERT, true);/* www . ja v a 2 s. c om*/ coll.update(Filters.eq("_id", id), document, options); }