Example usage for com.mongodb QueryBuilder QueryBuilder

List of usage examples for com.mongodb QueryBuilder QueryBuilder

Introduction

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

Prototype

public QueryBuilder() 

Source Link

Document

Creates a builder with an empty query

Usage

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

protected User updateImpl(org.mongoj.samples.model.User user) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    if (user.isNew()) {
        user.setNew(false);//w  w w.jav  a 2  s .  c om

        UserImpl userImpl = (UserImpl) user;

        userImpl.addMap.clear();
        userImpl.appendMap.clear();
        userImpl.removeMap.clear();
        userImpl.setMap.clear();

        WriteResult writeResult = collection.insert(getDBObject(user));

        String err = writeResult.getError();

        if (err != null) {
            throw new UpdateException(err);
        }
    } else {
        DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(user.getId())).get();

        UserImpl userImpl = (UserImpl) user;

        BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start();
        BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start();

        for (String field : userImpl.setMap.keySet()) {
            setUpdates = setUpdates.add(field, userImpl.setMap.get(field));
        }

        if (!setUpdates.isEmpty()) {
            updateBuilder.add(SET_OPERATOR, setUpdates.get());
        }

        for (String field : userImpl.appendMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.appendMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    pushUpdates = pushUpdates.add(field, ((List) userImpl.appendMap.get(field)).get(0));
                } else {
                    pushAllUpdates = pushAllUpdates.add(field, userImpl.appendMap.get(field));
                }
            }
        }

        if (!pushUpdates.isEmpty()) {
            updateBuilder.add(PUSH_OPERATOR, pushUpdates.get());
        }

        if (!pushAllUpdates.isEmpty()) {
            updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get());
        }

        for (String field : userImpl.addMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.addMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    addUpdates = addUpdates.add(field, ((List) userImpl.addMap.get(field)).get(0));
                } else {
                    DBObject each = BasicDBObjectBuilder.start()
                            .add(EACH_OPERATOR, ((List) userImpl.addMap.get(field)).toArray()).get();

                    addUpdates = addUpdates.add(field, each);
                }
            }
        }

        if (!addUpdates.isEmpty()) {
            updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get());
        }

        for (String field : userImpl.removeMap.keySet()) {
            List<Object> list = (List<Object>) userImpl.removeMap.get(field);

            if (!list.isEmpty()) {
                if (list.size() == 1) {
                    removeUpdates = removeUpdates.add(field, ((List) userImpl.removeMap.get(field)).get(0));
                } else {
                    removeAllUpdates = removeAllUpdates.add(field, userImpl.removeMap.get(field));
                }
            }
        }

        if (!removeUpdates.isEmpty()) {
            updateBuilder.add(PULL_OPERATOR, removeUpdates.get());
        }

        if (!removeAllUpdates.isEmpty()) {
            updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get());
        }

        if (!updateBuilder.isEmpty()) {
            DBObject update = updateBuilder.get();

            _log.debug("Update query = {}", update);

            WriteResult writeResult = collection.update(criteria, update);

            String err = writeResult.getError();

            if (err != null) {
                throw new UpdateException(err);
            }
        }
    }

    return user;
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

/**
 * Fetch a collection of dbrefs.  This is more efficient than fetching one at a time.
 *
 * @param collection the collection to fetch
 * @param fields     The fields to retrieve for each of the documents
 * @return The collection of referenced objcets
 *//*from  www .j  a v  a 2  s  .  com*/
public <R, RK> List<R> fetch(Collection<org.mongojack.DBRef<R, RK>> collection, DBObject fields) {
    Map<JacksonCollectionKey, List<Object>> collectionsToIds = new HashMap<JacksonCollectionKey, List<Object>>();
    for (org.mongojack.DBRef<R, RK> ref : collection) {
        if (ref instanceof FetchableDBRef) {
            JacksonCollectionKey key = ((FetchableDBRef) ref).getCollectionKey();
            List<Object> ids = collectionsToIds.get(key);
            if (ids == null) {
                ids = new ArrayList<Object>();
                collectionsToIds.put(key, ids);
            }
            ids.add(getReferenceCollection(key).convertToDbId(ref.getId()));
        }
    }
    List<R> results = new ArrayList<R>();
    for (Map.Entry<JacksonCollectionKey, List<Object>> entry : collectionsToIds.entrySet()) {
        for (R result : this.<R, RK>getReferenceCollection(entry.getKey())
                .find(new QueryBuilder().put("_id").in(entry.getValue()).get(), fields)) {
            results.add(result);
        }
    }
    return results;
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<String> biotypes = options.getAsStringList("biotype");
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*from  ww w  .java 2  s.com*/
        builder = builder.and("biotype").in(biotypeIds);
    }

    return executeQuery("result", new Document(builder.get().toMap()), options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    Document[] commands = new Document[2];
    Document unwind = new Document("$unwind", "$transcripts");
    commands[0] = unwind;//  w  w  w  .  j  ava  2s.  c  o m

    List<Object> biotypes = options.getList("biotypes", null);
    if (biotypes != null && biotypes.size() > 0) {

        //            Document match = new Document("$match", new Document("chunkIds", id));
        //            builder = builder.and("biotype").in(biotypeIds);

        //            commands[0] = match;
        commands[1] = unwind;
    } else {
        commands[0] = unwind;
    }

    return executeAggregation2("result", Arrays.asList(commands), options);
}

From source file:org.opencb.cellbase.lib.db.regulatory.RegulatoryRegionMongoDBAdaptor.java

License:Apache License

@Override
public List<QueryResult> getAllByRegionList(List<Region> regionList, QueryOptions options) {
    //  db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156})
    QueryBuilder builder = new QueryBuilder();

    List<Object> featureType = options.getAsList("featureType");
    List<Object> featureClass = options.getAsList("featureClass");

    //        options = addExcludeReturnFields("chunkIds", options);

    List<Document> queries = new ArrayList<>();
    for (Region region : regionList) {
        int firstChunkId = getChunkId(region.getStart(), regulatoryRegionChunkSize);
        int lastChunkId = getChunkId(region.getEnd(), regulatoryRegionChunkSize);
        BasicDBList chunksId = new BasicDBList();
        for (int j = firstChunkId; j <= lastChunkId; j++) {
            String chunkId = region.getChromosome() + "_" + j + "_" + regulatoryRegionChunkSize / 1000 + "k";
            chunksId.add(chunkId);//w  w w. j  a v a  2 s .co m
        }

        //            logger.info(chunksId.toString());

        builder = builder.start("_chunkIds").in(chunksId).and("start").lessThanEquals(region.getEnd())
                .and("end").greaterThanEquals(region.getStart());

        if (featureType != null && featureType.size() > 0) {
            BasicDBList featureTypeDBList = new BasicDBList();
            featureTypeDBList.addAll(featureType);
            builder = builder.and("featureType").in(featureTypeDBList);
        }

        if (featureClass != null && featureClass.size() > 0) {
            BasicDBList featureClassDBList = new BasicDBList();
            featureClassDBList.addAll(featureClass);
            builder = builder.and("featureClass").in(featureClassDBList);
        }

        queries.add(new Document(builder.get().toMap()));
    }
    //        System.out.println(">>"+regionList);
    //        System.out.println(">>"+new Document(builder.get().toMap()).toString());
    return executeQueryList2(regionList, queries, options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<Object> biotypes = options.getList("disease", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*from w  ww. j  av a2s . c  om*/
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

    return executeQuery("result", new Document(builder.get().toMap()), options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<String> biotypes = options.getAsStringList("biotype");
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*from  ww  w  .j  a va 2s . c  o m*/
        builder = builder.and("biotype").in(biotypeIds);
    }

    return executeQuery("result", builder.get(), options);
}

From source file:org.opencb.cellbase.mongodb.db.core.TranscriptMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    DBObject[] commands = new DBObject[2];
    DBObject unwind = new BasicDBObject("$unwind", "$transcripts");
    commands[0] = unwind;//from  w ww.  j a  va2  s .  com

    List<Object> biotypes = options.getList("biotypes", null);
    if (biotypes != null && biotypes.size() > 0) {

        //            DBObject match = new BasicDBObject("$match", new BasicDBObject("chunkIds", id));
        //            builder = builder.and("biotype").in(biotypeIds);

        //            commands[0] = match;
        commands[1] = unwind;
    } else {
        commands[0] = unwind;
    }

    //      options = addExcludeReturnFields("transcripts", options);
    return executeAggregation2("result", Arrays.asList(commands), options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<Object> biotypes = options.getList("biotypes", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);// w w  w  .ja va2s.  c  o m
        builder = builder.and("biotype").in(biotypeIds);
    }

    //      options = addExcludeReturnFields("transcripts", options);
    return executeQuery("result", builder.get(), options);
}

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

License:Apache License

@Override
public QueryResult getAll(QueryOptions options) {
    QueryBuilder builder = new QueryBuilder();

    List<Object> biotypes = options.getList("disease", null);
    if (biotypes != null && biotypes.size() > 0) {
        BasicDBList biotypeIds = new BasicDBList();
        biotypeIds.addAll(biotypes);/*from   w  ww  . j a v  a 2  s . c om*/
        builder = builder.and("primaryHistology").in(biotypeIds);
    }

    return executeQuery("result", builder.get(), options);
}