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() 

Source Link

Document

Returns a new QueryBuilder.

Usage

From source file:org.entcore.workspace.service.impl.DefaultFolderService.java

License:Open Source License

@Override
public void list(String path, UserInfos owner, boolean hierarchical, String filter,
        final Handler<Either<String, JsonArray>> results) {
    if (owner == null) {
        results.handle(new Either.Left<String, JsonArray>("workspace.invalid.user"));
        return;/*  w  w w. j a  v  a 2s  .  c o m*/
    }

    QueryBuilder q = QueryBuilder.start();

    if ("shared".equals(filter)) {
        List<DBObject> groups = new ArrayList<>();
        groups.add(QueryBuilder.start("userId").is(owner.getUserId()).get());
        for (String gpId : owner.getGroupsIds()) {
            groups.add(QueryBuilder.start("groupId").is(gpId).get());
        }
        q.put("shared").elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get());
    } else {
        q.put("owner").is(owner.getUserId());
    }

    q.and("file").exists(false).and("application").is(WorkspaceService.WORKSPACE_NAME);

    if (path != null && !path.trim().isEmpty()) {
        if (hierarchical) {
            q = q.put("folder").regex(Pattern.compile("^" + Pattern.quote(path) + "_[^_]+$"));
        } else {
            q = q.put("folder").regex(Pattern.compile("^" + Pattern.quote(path) + "_"));
        }
    } else if (hierarchical) {
        q = q.put("folder").regex(Pattern.compile("^[^_]+$"));
    }
    mongo.find(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> res) {
            results.handle(Utils.validResults(res));
        }
    });
}

From source file:org.mongodb.workshop.api.ComicsService.java

License:Apache License

@GET
@Path("/{id}")
@ApiOperation(value = "Return one Comic based on his _id")
public DBObject get(@PathParam("id") int id) {
    DBObject query = QueryBuilder.start().put("_id").is(id).get();
    return comicsCollection.findOne(query);
}

From source file:org.mongodb.workshop.api.ComicsService.java

License:Apache License

@GET
@Path("/search")
@ApiOperation(value = "Full text search on comics")
public DBObject search(@DefaultValue("1") @QueryParam("page") int page, @QueryParam("keyword") String keyword) {

    DBObject query = QueryBuilder.start().text(keyword).get();

    DBObject proj = BasicDBObjectBuilder.start().append("title", 1).append("description", 1).push("score")
            .append("$meta", "textScore").get();

    DBObject sort = BasicDBObjectBuilder.start().push("score").append("$meta", "textScore").get();

    long count = comicsCollection.count(query);
    int skip = ITEMS_PER_PAGE * (page - 1);

    DBCursor cursor = comicsCollection.find(query, proj).sort(sort).skip(skip).limit(ITEMS_PER_PAGE);

    List<DBObject> items = cursor.toArray(cursor.size());

    ResultDBObject result = new ResultDBObject(count, ITEMS_PER_PAGE, page, items);

    return result;
}

From source file:org.mongodb.workshop.api.CreatorsService.java

License:Apache License

@GET
@Path("/{id}")
@ApiOperation(value = "Return one Comic based on his _id")
public DBObject get(@PathParam("id") int id) {
    DBObject query = QueryBuilder.start().put("_id").is(id).get();
    return creatorsCollection.findOne(query);
}

From source file:org.mongodb.workshop.api.CreatorsService.java

License:Apache License

@GET
@Path("/search")
@ApiOperation(value = "Full text search on Creators")
public DBObject search(@DefaultValue("1") @QueryParam("page") int page,
        @QueryParam("comicsName") String comicsName) {

    DBObject query = QueryBuilder.start().put("comics.items.name")
            .regex(java.util.regex.Pattern.compile(comicsName, Pattern.CASE_INSENSITIVE)).get();

    long count = creatorsCollection.count(query);
    int skip = ITEMS_PER_PAGE * (page - 1);
    DBCursor cursor = creatorsCollection.find(query).skip(skip).limit(ITEMS_PER_PAGE);
    List<DBObject> items = cursor.toArray(cursor.size());
    ResultDBObject result = new ResultDBObject(count, ITEMS_PER_PAGE, page, items);
    return result;
}

From source file:org.opencb.cellbase.lib.db.variation.ClinicalMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    System.out.println("options = " + options.get("exclude"));
    QueryBuilder builder = QueryBuilder.start();
    return executeQuery("result", getFilters(options), options);
}

From source file:org.opencb.cellbase.lib.db.variation.ClinVarMongoDBAdaptor.java

License:Apache License

public QueryResult getListAccessions(QueryOptions queryOptions) {
    QueryBuilder builder = QueryBuilder.start();
    queryOptions.put("include", Arrays.asList("referenceClinVarAssertion.clinVarAccession.acc"));
    QueryResult queryResult = executeQuery("", new Document(builder.get().toMap()), queryOptions);
    BasicDBList accInfoList = (BasicDBList) queryResult.getResult();
    List<String> accList = new ArrayList<>(accInfoList.size());
    Document accInfo;/*w ww  .  j av a 2 s.  com*/
    QueryResult listAccessionsToReturn = new QueryResult();

    for (Object accInfoObject : accInfoList) {
        accInfo = (Document) accInfoObject;
        accInfo = (Document) accInfo.get("referenceClinVarAssertion");
        accInfo = (Document) accInfo.get("clinVarAccession");
        accList.add((String) accInfo.get("acc"));
    }

    // setting listAccessionsToReturn fields
    listAccessionsToReturn.setId(queryResult.getId());
    listAccessionsToReturn.setDbTime(queryResult.getDbTime());
    listAccessionsToReturn.setNumResults(queryResult.getNumResults());
    listAccessionsToReturn.setResult(accList);

    return listAccessionsToReturn;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarPhenotypeFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> phenotypeList = options.getList("phenotype", null);
    if (phenotypeList != null && phenotypeList.size() > 0) {
        QueryBuilder phenotypeQueryBuilder = QueryBuilder.start();
        for (Object phenotype : phenotypeList) {
            String phenotypeString = (String) phenotype;
            phenotypeQueryBuilder = phenotypeQueryBuilder
                    .or(QueryBuilder.start("referenceClinVarAssertion.traitSet.trait.name.elementValue.value")
                            .text(phenotypeString).get());
        }//from  w w  w  .  ja va2s  .c  o  m
        builder = builder.and(phenotypeQueryBuilder.get());
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinicalMongoDBAdaptor.java

License:Apache License

private QueryBuilder addClinvarIdFilter(QueryBuilder builder, QueryOptions options) {
    List<Object> idList = options.getList("id", null);
    if (idList != null && idList.size() > 0) {
        QueryBuilder idQueryBuilder = QueryBuilder.start();
        for (Object id : idList) {
            String idString = (String) id;
            if (idString.toLowerCase().startsWith("rs")) {
                idQueryBuilder = idQueryBuilder.or(QueryBuilder.start(
                        "clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.type")
                        .is("rs")
                        .and("clinvarList.clinvarSet.referenceClinVarAssertion.measureSet.measure.attributeSet.xref.id")
                        .is(Integer.valueOf(idString.substring(2))).get());
            } else if (idString.toLowerCase().startsWith("rcv")) {
                idQueryBuilder = idQueryBuilder.or(QueryBuilder
                        .start("clinvarList.clinvarSet.referenceClinVarAssertion.clinVarAccession.acc")
                        .is(idString).get());
            }//w  w  w . j a v  a 2 s .  co  m
        }
        builder = builder.and(idQueryBuilder.get());
    }
    return builder;
}

From source file:org.opencb.cellbase.mongodb.db.ClinVarMongoDBAdaptor.java

License:Apache License

public QueryResult getListAccessions(QueryOptions queryOptions) {
    QueryBuilder builder = QueryBuilder.start();
    queryOptions.put("include", Arrays.asList("referenceClinVarAssertion.clinVarAccession.acc"));
    QueryResult queryResult = executeQuery("", builder.get(), queryOptions);
    BasicDBList accInfoList = (BasicDBList) queryResult.getResult();
    List<String> accList = new ArrayList<>(accInfoList.size());
    BasicDBObject accInfo;//from  w w w.  j a  v  a2 s .  c  om
    QueryResult listAccessionsToReturn = new QueryResult();

    for (Object accInfoObject : accInfoList) {
        accInfo = (BasicDBObject) accInfoObject;
        accInfo = (BasicDBObject) accInfo.get("referenceClinVarAssertion");
        accInfo = (BasicDBObject) accInfo.get("clinVarAccession");
        accList.add((String) accInfo.get("acc"));
    }

    // setting listAccessionsToReturn fields
    listAccessionsToReturn.setId(queryResult.getId());
    listAccessionsToReturn.setDbTime(queryResult.getDbTime());
    listAccessionsToReturn.setNumResults(queryResult.getNumResults());
    listAccessionsToReturn.setResult(accList);

    return listAccessionsToReturn;
}