Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:org.mongodb.demos.binary.JsonDemo.java

License:Apache License

/**
 * Read employee and save the file into the location
 * @param id/*from   w  w  w  . j a  v  a 2  s . co  m*/
 * @param location
 * @throws Exception
 */
public void readEmployee(String id, String location) throws Exception {
    DBObject emp = fooCollection.findOne(QueryBuilder.start("_id").is(id).get());

    System.out.println("\n== ==  READ FROM MONGODB  == == == ");
    System.out.println(emp);

    byte[] imageAsBytes = (byte[]) emp.get("image");
    String fileCreated = saveImageToFile(imageAsBytes, location);

    System.out.println("\n\t File saved at : " + location + fileCreated);
    System.out.println("\n== == == == == ");

}

From source file:org.mongodb.demos.obj.MyAppDriver.java

License:Apache License

private void findPersons() {

    System.out.println(" == == == Query == == == ");
    DBObject query = QueryBuilder.start("age").is(35).get();
    System.out.println("\tfilter : " + query);

    for (DBObject dbo : fooCollection.find(query)) {
        System.out.println(dbo);//from  w  ww .ja  va 2s.c  o m
    }

    System.out.println(" == == == ===  == == == ");

}

From source file:org.mule.module.mongo.MongoObjectStore.java

License:Open Source License

public void expire(final int entryTtl, final int ignoredMaxEntries, final String partitionName)
        throws ObjectStoreException {
    final String collection = getCollectionName(partitionName);
    final long expireAt = System.currentTimeMillis() - entryTtl;
    final DBObject query = QueryBuilder.start(TIMESTAMP_FIELD).lessThan(expireAt).get();
    mongoClient.removeObjects(collection, query, getWriteConcern());
}

From source file:org.opencb.cellbase.lib.db.core.ConservationMongoDBAdaptor.java

License:Apache License

@Deprecated
@Override//from  ww w .  j a v a 2s.  co m
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<String> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        // Max region size is 10000bp
        if (region.getEnd() - region.getStart() > 10000) {
            region.setEnd(region.getStart() + 10000);
        }

        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), this.chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            ////                    integerChunkIds.add(chunkId);
            //                    integerChunkIds.add(region.getChromosomeInfo() + "_" + chunkId + "_" + this.chunkSize/1000 + "k");
            //                }
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(hunkIds);
        /****/

        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());

        //            logger.debug(builder.get().toString());
    }

    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        //            BasicDBList list = (BasicDBList) queryResult.getResult();
        List list = queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            Document chunk = (Document) list.get(j);
            String source = chunk.getString("source");
            List<Float> valuesList;
            if (!typeMap.containsKey(source)) {
                valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                    valuesList.add(null);
                }
                typeMap.put(source, valuesList);
            } else {
                valuesList = typeMap.get(source);
            }

            BasicDBList valuesChunk = (BasicDBList) chunk.get("values");

            int pos = 0;
            if (region.getStart() > chunk.getInteger("start")) {
                pos = region.getStart() - chunk.getInteger("start");
            }

            for (; pos < valuesChunk.size() && (pos + chunk.getInteger("start") <= region.getEnd()); pos++) {
                //                    System.out.println("valuesList SIZE = " + valuesList.size());
                //                    System.out.println("pos = " + pos);
                //                    System.out.println("DIV " + (chunk.getInt("start") - region.getStart()));
                //                    System.out.println("valuesChunk = " + valuesChunk.get(pos));
                //                    System.out.println("indexFinal = " + (pos + chunk.getInt("start") - region.getStart()));
                valuesList.set(pos + chunk.getInteger("start") - region.getStart(),
                        new Float((Double) valuesChunk.get(pos)));
            }
        }
        //
        BasicDBList resultList = new BasicDBList();
        //            ConservationScoreRegion conservedRegionChunk;
        GenomicScoreRegion<Float> conservedRegionChunk;
        for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
            //                conservedRegionChunk = new ConservationScoreRegion(region.getChromosome(), region.getStart(),
            //                        region.getEnd(), elem.getKey(), elem.getValue());
            conservedRegionChunk = new GenomicScoreRegion<>(region.getChromosome(), region.getStart(),
                    region.getEnd(), elem.getKey(), elem.getValue());
            resultList.add(conservedRegionChunk);
        }
        queryResult.setResult(resultList);
    }

    return queryResults;
}

From source file:org.opencb.cellbase.lib.db.core.ConservationMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllScoresByRegionList(List<Region> regions, QueryOptions options) {
    //TODO not finished yet
    List<Document> queries = new ArrayList<>();
    List<String> ids = new ArrayList<>(regions.size());
    List<Integer> integerChunkIds;
    for (Region region : regions) {
        integerChunkIds = new ArrayList<>();
        // positions below 1 are not allowed
        if (region.getStart() < 1) {
            region.setStart(1);/*from  w  w  w  . j  av  a 2  s  . c om*/
        }
        if (region.getEnd() < 1) {
            region.setEnd(1);
        }

        /****/
        QueryBuilder builder;
        int regionChunkStart = getChunkId(region.getStart(), this.chunkSize);
        int regionChunkEnd = getChunkId(region.getEnd(), this.chunkSize);
        if (regionChunkStart == regionChunkEnd) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), chunkSize));
        } else {
            //                for (int chunkId = regionChunkStart; chunkId <= regionChunkEnd; chunkId++) {
            //                    integerChunkIds.add(chunkId);
            //                }
            //    //            QueryBuilder builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(hunkIds);
            //                builder = QueryBuilder.start("chromosome").is(region.getChromosomeInfo()).and("chunkId").in(integerChunkIds);
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }
        /****/

        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());

        //            logger.debug(builder.get().toString());

    }
    List<QueryResult> queryResults = executeQueryList2(ids, queries, options);
    //        List<QueryResult> queryResults = executeQueryList(ids, queries, options);

    for (int i = 0; i < regions.size(); i++) {
        Region region = regions.get(i);
        QueryResult queryResult = queryResults.get(i);
        List<Document> list = (List<Document>) queryResult.getResult();

        Map<String, List<Float>> typeMap = new HashMap();

        //            int start = region.getStart();

        for (int j = 0; j < list.size(); j++) {
            Document chunk = list.get(j);

            if (!chunk.isEmpty()) {
                BasicDBList valuesChunk = (BasicDBList) chunk.get("values");
                if (valuesChunk != null) { // TODO: temporary patch to skip empty chunks - remove as soon as conservation is reloaded
                    String source = chunk.getString("source");
                    List<Float> valuesList;
                    if (!typeMap.containsKey(source)) {
                        valuesList = new ArrayList<>(region.getEnd() - region.getStart() + 1);
                        for (int val = 0; val < region.getEnd() - region.getStart() + 1; val++) {
                            valuesList.add(null);
                        }
                        typeMap.put(source, valuesList);
                    } else {
                        valuesList = typeMap.get(source);
                    }

                    valuesChunk = (BasicDBList) chunk.get("values");
                    int pos = 0;
                    if (region.getStart() > chunk.getInteger("start")) {
                        pos = region.getStart() - chunk.getInteger("start");
                    }

                    for (; pos < valuesChunk.size()
                            && (pos + chunk.getInteger("start") <= region.getEnd()); pos++) {
                        valuesList.set(pos + chunk.getInteger("start") - region.getStart(),
                                new Float((Double) valuesChunk.get(pos)));
                    }
                } else {
                    continue;
                }

            }

            BasicDBList resultList = new BasicDBList();
            for (Map.Entry<String, List<Float>> elem : typeMap.entrySet()) {
                for (Float value : elem.getValue()) {
                    if (value != null) {
                        resultList.add(new Score(new Double(value), elem.getKey()));
                    }
                }
            }
            if (!resultList.isEmpty()) {
                queryResult.setResult(resultList);
            } else {
                queryResult.setResult(null);
            }
        }
    }
    return queryResults;
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) {
    List<Document> queries = new ArrayList<>(idList.size());
    for (String id : idList) {
        QueryBuilder builder = QueryBuilder.start("transcripts.xrefs.id").is(id);
        queries.add(new Document(builder.get().toMap()));
    }//from w ww  . jav  a  2  s  .  co m

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);
    return executeQueryList2(idList, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getStatsById(String id, QueryOptions options) {

    Map<String, Object> stats = new HashMap<>();
    QueryResult queryResult = new QueryResult();
    queryResult.setId(id);// w  w  w .j av a2s. c  om

    QueryBuilder geneBuilder = QueryBuilder.start("transcripts.xrefs.id").is(id);
    long dbTimeStart = System.currentTimeMillis();
    QueryResult geneQueryResult = executeQuery(id, new Document(geneBuilder.get().toMap()), new QueryOptions());
    // TODO: clinical variant summary is only provided for ClinVar (hardcoded below)
    QueryOptions clinicalQueryOptions = new QueryOptions("source", "clinvar");
    clinicalQueryOptions.put("include",
            "annot.consequenceTypes.soTerms,clinvarSet.referenceClinVarAssertion.clinicalSignificance.description");
    QueryResult clinicalQueryResult = clinicalDBAdaptor.getByGeneId(id, clinicalQueryOptions);
    long dbTimeEnd = System.currentTimeMillis();
    queryResult.setDbTime(Long.valueOf(dbTimeEnd - dbTimeStart).intValue());

    if (geneQueryResult.getNumResults() > 0) {
        queryResult.setNumResults(1);
        stats = setCoreGeneStats(geneQueryResult, stats);
        stats = setVariantStats(clinicalQueryResult, stats);
        queryResult.setResult(Collections.singletonList(stats));
    }

    return queryResult;
    //        gene name
    //        ensembl gene id
    //        chr
    //        start
    //        end
    //        sequence length
    //        num transcripts
    //        breakdown num transcripts by biotype
    //        num exons
    //        num drug interactions
    //        Clinical Variants {
    //            #
    //            Breakdown by clinical significance
    //            Breakdown by SO
    //        }

    //        options = addExcludeReturnFields("transcripts", options);
    //        return executeQueryList(idList, queries, options);

}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getAllBiotypes(QueryOptions options) {
    Document query = null;//from  w ww. ja  v a 2s.  com

    if (options != null && options.get("chromosome") != null) {
        query = new Document(QueryBuilder.start("chromosome").is(options.get("chromosome")).get().toMap());
    }
    return executeDistinct("distinct", "biotype", query);
}

From source file:org.opencb.cellbase.lib.db.core.GeneMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regions, QueryOptions options) {
    List<Document> queries = new ArrayList<>();

    List<Object> biotypes = options.getList("biotype", null);
    BasicDBList biotypeIds = new BasicDBList();
    if (biotypes != null && biotypes.size() > 0) {
        biotypeIds.addAll(biotypes);/*from   w  w  w  . j  ava2 s.c o  m*/
    }

    List<String> ids = new ArrayList<>(regions.size());
    for (Region region : regions) {

        QueryBuilder builder = null;
        // If regions is 1 position then query can be optimize using chunks
        if (region.getStart() == region.getEnd()) {
            builder = QueryBuilder.start("_chunkIds")
                    .is(getChunkIdPrefix(region.getChromosome(), region.getStart(), geneChunkSize)).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        } else {
            builder = QueryBuilder.start("chromosome").is(region.getChromosome()).and("end")
                    .greaterThanEquals(region.getStart()).and("start").lessThanEquals(region.getEnd());
        }

        if (biotypeIds.size() > 0) {
            builder = builder.and("biotype").in(biotypeIds);
        }
        queries.add(new Document(builder.get().toMap()));
        ids.add(region.toString());
    }
    return executeQueryList2(ids, queries, options);
    //        return executeQueryList(ids, queries, options);
}

From source file:org.opencb.cellbase.lib.db.core.GenomeMongoDBAdaptor.java

License:Apache License

@Deprecated
public QueryResult speciesInfoTmp(String id, QueryOptions options) {
    // reading application.properties file

    //        String[] speciesArray = applicationProperties.getProperty("SPECIES").split(",");

    //        List<Document> queries = new ArrayList<>(1);
    //        for (String id : idList) {
    QueryBuilder builder = QueryBuilder.start("species").is(id);

    //        queries.add(builder.get());
    //        }/*from   w  w w  .  ja v  a 2 s  . c  om*/

    //        options = addExcludeReturnFields("transcripts", options);
    return executeQuery(id, new Document(builder.get().toMap()), options);

}