Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

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

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantBeforeInterval(
        final TemporalInterval givenInterval, final StatementConstraints constraints)
        throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT)
            .lessThan(givenInterval.getHasBeginning().getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantInsideInterval(
        final TemporalInterval givenInterval, final StatementConstraints constraints)
        throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT)
            .greaterThan(givenInterval.getHasBeginning().getAsDateTime().toDate())
            .lessThan(givenInterval.getHasEnd().getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalEquals(final TemporalInterval query,
        final StatementConstraints constraints) throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INTERVAL_START)
            .is(query.getHasBeginning().getAsDateTime().toDate()).and(INTERVAL_END)
            .is(query.getHasEnd().getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalBefore(final TemporalInterval query,
        final StatementConstraints constraints) throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INTERVAL_END)
            .lessThan(query.getHasBeginning().getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalAfter(final TemporalInterval query,
        final StatementConstraints constraints) throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INTERVAL_START)
            .greaterThan(query.getHasEnd().getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.einherjer.week2.samples.DotNotationSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection lines = db.getCollection("dotNotationSample");
    lines.drop();/*from  w w w  .  ja  va 2  s. c o m*/
    Random rand = new Random();

    // insert 10 lines with random start and end points
    for (int i = 0; i < 10; i++) {
        lines.insert(
                new BasicDBObject("_id", i)
                        .append("start",
                                new BasicDBObject("x", rand.nextInt(90) + 10).append("y",
                                        rand.nextInt(90) + 10))
                        .append("end", new BasicDBObject("x", rand.nextInt(90) + 10).append("y",
                                rand.nextInt(90) + 10)));
    }

    QueryBuilder builder = QueryBuilder.start("start.x").greaterThan(50);

    DBCursor cursor = lines.find(builder.get(), new BasicDBObject("start.y", true).append("_id", false));

    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }
}

From source file:org.einherjer.week2.samples.FindCriteriaSample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    Mongo client = new Mongo();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findCriteriaSample");
    collection.drop();// ww w . j a  va2  s .  c  om

    // insert 10 documents with two random integers
    for (int i = 0; i < 10; i++) {
        collection
                .insert(new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100)));
    }

    //1- The query document can be created by using a QueryBuilder...
    QueryBuilder builder = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(70);
    //2- or, the query document can be created manually
    DBObject query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt", 10).append("$lt", 90));

    System.out.println("\nCount:");
    long count = collection.count(builder.get());
    System.out.println(count);

    System.out.println("\nFind all: ");
    DBCursor cursor = collection.find(builder.get());
    try {
        while (cursor.hasNext()) {
            DBObject cur = cursor.next();
            System.out.println(cur);
        }
    } finally {
        cursor.close();
    }
}

From source file:org.entcore.blog.events.BlogSearchingEvents.java

License:Open Source License

private void searchPosts(int page, int limit, List<String> searchWords, final Set<String> setIds,
        Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final QueryBuilder worldsQuery = new QueryBuilder();
    worldsQuery.text(MongoDbSearchService.textSearchedComposition(searchWords));

    final QueryBuilder blogQuery = new QueryBuilder().start("blog.$id").in(setIds);
    final QueryBuilder publishedQuery = new QueryBuilder().start("state").is(PUBLISHED_STATE);

    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), blogQuery.get(), publishedQuery.get());

    JsonObject sort = new JsonObject().put("modified", -1);
    final JsonObject projection = new JsonObject();
    projection.put("title", 1);
    projection.put("content", 1);
    projection.put("blog.$id", 1);
    projection.put("modified", 1);
    projection.put("author.userId", 1);
    projection.put("author.username", 1);

    mongo.find(Blog.POSTS_COLLECTION, MongoQueryBuilder.build(query), sort, projection, skip, limit,
            Integer.MAX_VALUE, validResultsHandler(handler));
}

From source file:org.entcore.common.service.impl.MongoDbSearchService.java

License:Open Source License

@Override
public void search(String userId, List<String> groupIds, List<String> returnFields, List<String> searchWords,
        Integer page, Integer limit, Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final List<DBObject> groups = new ArrayList<DBObject>();
    groups.add(QueryBuilder.start("userId").is(userId).get());
    for (String gpId : groupIds) {
        groups.add(QueryBuilder.start("groupId").is(gpId).get());
    }/*from   w  w w .  jav  a2s .  c  om*/

    final QueryBuilder worldsQuery = new QueryBuilder();
    //no stemming (in fact, stemming works only with words and for a given language) and no list of stop words
    worldsQuery.text(textSearchedComposition(searchWords));

    final QueryBuilder rightsOrQuery = new QueryBuilder().or(
            QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(),
            QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
            QueryBuilder.start(this.ownerUserId).is(userId).get(), QueryBuilder.start("shared")
                    .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()).get());

    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), rightsOrQuery.get());

    JsonObject sort = new JsonObject().put("modified", -1);
    final JsonObject projection = new JsonObject();
    for (String field : returnFields) {
        projection.put(field, 1);
    }

    mongo.find(collection, MongoQueryBuilder.build(query), sort, projection, skip, limit, Integer.MAX_VALUE,
            validResultsHandler(handler));
}

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

License:Open Source License

@Override
public void trash(String id, final UserInfos owner, final Handler<Either<String, JsonObject>> result) {
    if (owner == null) {
        result.handle(new Either.Left<String, JsonObject>("workspace.invalid.user"));
        return;//w  w w . ja v  a2  s.  c om
    }
    if (id == null || id.trim().isEmpty()) {
        result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found"));
        return;
    }
    final QueryBuilder resourceQuery = QueryBuilder.start("_id").is(id);

    final 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());
    }

    final QueryBuilder rightQuery = new QueryBuilder()
            .or(QueryBuilder.start("owner").is(owner.getUserId()).get(), QueryBuilder.start("shared")
                    .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()).get());

    final QueryBuilder query = new QueryBuilder().and(resourceQuery.get(), rightQuery.get());

    JsonObject keys = new JsonObject().put("folder", 1).put("name", 1).put("owner", 1);
    mongo.findOne(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(query), keys,
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> event) {
                    final String folder = event.body().getJsonObject("result", new JsonObject())
                            .getString("folder");
                    final String name = event.body().getJsonObject("result", new JsonObject())
                            .getString("name");
                    final String owner = event.body().getJsonObject("result", new JsonObject())
                            .getString("owner");
                    if ("ok".equals(event.body().getString("status")) && folder != null
                            && !folder.trim().isEmpty()) {
                        QueryBuilder q = QueryBuilder.start("owner").is(owner).put("folder")
                                .regex(Pattern.compile("^" + Pattern.quote(folder) + "($|_)"));
                        MongoUpdateBuilder modifier = new MongoUpdateBuilder();
                        modifier.rename("folder", "old-folder");
                        mongo.update(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), modifier.build(), false,
                                true, new Handler<Message<JsonObject>>() {
                                    @Override
                                    public void handle(Message<JsonObject> e) {
                                        if ("ok".equals(e.body().getString("status"))) {
                                            recursiveMove(folder, name, owner, "Trash", result);
                                        } else {
                                            result.handle(new Either.Left<String, JsonObject>(
                                                    "workspace.trash.error"));
                                        }
                                    }
                                });
                    } else {
                        result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found"));
                    }
                }
            });
}