com.mongodb.QueryOperators Java Examples

The following examples show how to use com.mongodb.QueryOperators. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Support.java    From immutables with Apache License 2.0 6 votes vote down vote up
private Document mergeConjunctions(List<Document> conjunctions) {
  final Multimap<String, Document> merged = LinkedHashMultimap.create();

  for (Document doc : conjunctions) {
    Preconditions.checkState(doc.keySet().size() == 1, "Invalid constraint %s", doc);
    final String key = doc.keySet().iterator().next();
    merged.put(key, doc);
  }

  final Document result = new Document();

  for (Map.Entry<String, Collection<Document>> entry : merged.asMap().entrySet()) {
    Preconditions.checkState(!entry.getValue().isEmpty(), "empty constraint: %s", entry);

    if (entry.getValue().size() == 1) {
      result.putAll(entry.getValue().iterator().next());
    } else {
      result.putAll(new Document(QueryOperators.AND, entry.getValue()));
    }
  }

  return result;
}
 
Example #2
Source File: RenderDao.java    From render with GNU General Public License v2.0 6 votes vote down vote up
void removeTilesWithIds(final StackId stackId,
                        final List<String> tileIds)
        throws IllegalArgumentException {

    MongoUtil.validateRequiredParameter("stackId", stackId);
    MongoUtil.validateRequiredParameter("tileIds", tileIds);

    final MongoCollection<Document> tileCollection = getTileCollection(stackId);
    final Document tileQuery = new Document("tileId",
                                            new Document(QueryOperators.IN,
                                                         tileIds));
    final Document tileQueryForLog = new Document("tileId",
                                                  new Document(QueryOperators.IN,
                                                               Arrays.asList("list of",
                                                                             tileIds.size() + " tileIds")));
    final DeleteResult removeResult = tileCollection.deleteMany(tileQuery);

    LOG.debug("removeTilesWithIds: {}.remove({}) deleted {} document(s)",
              MongoUtil.fullName(tileCollection), tileQueryForLog.toJson(), removeResult.getDeletedCount());
}
 
Example #3
Source File: RenderDao.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private List<TransformSpec> getTransformSpecs(final MongoCollection<Document> transformCollection,
                                              final Set<String> specIds) {
    final int specCount = specIds.size();
    final List<TransformSpec> transformSpecList = new ArrayList<>(specCount);
    if (specCount > 0) {

        final Document transformQuery = new Document();
        transformQuery.put("id", new Document(QueryOperators.IN, specIds));

        LOG.debug("getTransformSpecs: {}.find({})",
                  MongoUtil.fullName(transformCollection), transformQuery.toJson());

        try (final MongoCursor<Document> cursor = transformCollection.find(transformQuery).iterator()) {
            Document document;
            TransformSpec transformSpec;
            while (cursor.hasNext()) {
                document = cursor.next();
                transformSpec = TransformSpec.fromJson(document.toJson());
                transformSpecList.add(transformSpec);
            }
        }

    }

    return transformSpecList;
}
 
Example #4
Source File: QueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Equivalent to a $within operand, based on a bounding polygon represented by an array of points
 *
 * @param points an array of Double[] defining the vertices of the search area
 * @return {@code this}
 */
public QueryBuilder withinPolygon(final List<Double[]> points) {
    notNull("points", points);
    if (points.isEmpty() || points.size() < 3) {
        throw new IllegalArgumentException("Polygon insufficient number of vertices defined");
    }
    addOperand(QueryOperators.WITHIN,
               new Document(QueryOperators.POLYGON, convertToListOfLists(points)));
    return this;
}
 
Example #5
Source File: QueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Equivalent to a $text operand.
 *
 * @param search   the search terms to apply to the text index.
 * @param language the language to use.
 * @return {@code this}
 * @mongodb.server.release 2.6
 */
public QueryBuilder text(final String search, @Nullable final String language) {
    if (currentKey != null) {
        throw new QueryBuilderException("The text operand may only occur at the top-level of a query. It does"
                                        + " not apply to a specific element, but rather to a document as a whole.");
    }

    put(QueryOperators.TEXT);
    addOperand(QueryOperators.SEARCH, search);
    if (language != null) {
        addOperand(QueryOperators.LANGUAGE, language);
    }

    return this;
}
 
Example #6
Source File: QueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Equivalent to an $or operand
 *
 * @param ors the list of conditions to or together
 * @return {@code this}
 */
public QueryBuilder or(final Document... ors) {
    List<Object> l = query.getList(QueryOperators.OR, Object.class);
    if (l == null) {
        l = new ArrayList<>();
        query.put(QueryOperators.OR, l);
    }
    Collections.addAll(l, ors);
    return this;
}
 
Example #7
Source File: QueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Equivalent to an $and operand
 *
 * @param ands the list of conditions to and together
 * @return {@code this}
 */
public QueryBuilder and(final Document... ands) {
    List<Object> l = query.getList(QueryOperators.AND, Object.class);
    if (l == null) {
        l = new ArrayList<>();
        query.put(QueryOperators.AND, l);
    }
    Collections.addAll(l, ands);
    return this;
}
 
Example #8
Source File: RenderDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
public void cloneStack(final StackId fromStackId,
                       final StackId toStackId,
                       final List<Double> zValues,
                       final Boolean skipTransforms)
        throws IllegalArgumentException, IllegalStateException {

    MongoUtil.validateRequiredParameter("fromStackId", fromStackId);
    MongoUtil.validateRequiredParameter("toStackId", toStackId);

    if ((skipTransforms == null) || (! skipTransforms)) {
        final MongoCollection<Document> fromTransformCollection = getTransformCollection(fromStackId);
        final MongoCollection<Document> toTransformCollection = getTransformCollection(toStackId);
        cloneCollection(fromTransformCollection, toTransformCollection, new Document());
    }

    final Document filterQuery = new Document();
    if ((zValues != null) && (zValues.size() > 0)) {
        final BasicDBList list = new BasicDBList();
        list.addAll(zValues);
        final Document zFilter = new Document(QueryOperators.IN, list);
        filterQuery.append("z", zFilter);
    }

    final MongoCollection<Document> fromTileCollection = getTileCollection(fromStackId);
    final MongoCollection<Document> toTileCollection = getTileCollection(toStackId);
    cloneCollection(fromTileCollection, toTileCollection, filterQuery);
}
 
Example #9
Source File: MatchDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
private Document getOutsideGroupQuery(final String groupId) {
    final List<Document> queryList = new ArrayList<>();
    queryList.add(new Document("pGroupId", groupId).append(
            "qGroupId", new Document(QueryOperators.NE, groupId)));
    queryList.add(new Document("qGroupId", groupId).append(
            "pGroupId", new Document(QueryOperators.NE, groupId)));
    return new Document(QueryOperators.OR, queryList);
}
 
Example #10
Source File: MatchDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
private Document getInvolvingObjectQuery(final String groupId,final String id){
    final List<Document> queryList = new ArrayList<>();
    queryList.add(new Document("pGroupId", groupId).append(
            "pId", id));
    queryList.add(new Document("qGroupId", groupId).append(
            "qId", id));
    return new Document(QueryOperators.OR, queryList);
}
 
Example #11
Source File: MatchDao.java    From render with GNU General Public License v2.0 5 votes vote down vote up
private Document getInvolvingObjectAndGroupQuery(final String groupId,final String id, final String qGroupId){
   final List<Document> queryList = new ArrayList<>();
     queryList.add(new Document("pGroupId", groupId).append(
             "pId", id).append("qGroupId",qGroupId));
     queryList.add(new Document("qGroupId", groupId).append(
             "qId", id).append("pGroupId",qGroupId));

    return new Document(QueryOperators.OR, queryList);
}
 
Example #12
Source File: Support.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public ConstraintBuilder in(String name, boolean negate, Iterable<?> values) {
  addContraint(name,
      new Document(
          negate ? QueryOperators.NIN : QueryOperators.IN,
          ImmutableSet.copyOf(unwrapBsonableIterable(values))));
  return this;
}
 
Example #13
Source File: MatchDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
private Set<String> getMultiConsensusGroupIds(final MatchCollectionId collectionId,
                                              final boolean includeQGroupIds)
        throws IllegalArgumentException {

    final MongoCollection<Document> matchCollection = getExistingCollection(collectionId);

    final List<Document> pipeline = new ArrayList<>();
    pipeline.add(new Document("$match",
                              new Document("consensusSetData",
                                           new Document(QueryOperators.EXISTS, true))));

    if (includeQGroupIds) {

        // db.<matchCollection>.aggregate(
        //     [
        //         { "$match": { "consensusSetData": { "$exists": true } } },
        //         { "$group": { "_id": { "pGroupId": "$pGroupId", "qGroupId": "$qGroupId" } } }
        //     ]
        // )

        pipeline.add(new Document("$group",
                                  new Document("_id",
                                               new Document("pGroupId", "$pGroupId").
                                                       append("qGroupId", "$qGroupId"))));
    } else {

        // db.<matchCollection>.aggregate(
        //     [
        //         { "$match": { "consensusSetData": { "$exists": true } } },
        //         { "$group": { "_id": { "pGroupId": "$pGroupId" } } }
        //     ]
        // )

        pipeline.add(new Document("$group",
                                  new Document("_id",
                                               new Document("pGroupId", "$pGroupId"))));
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("getMultiConsensusGroupIds: running {}.aggregate({})",
                  MongoUtil.fullName(matchCollection),
                  MongoUtil.toJson(pipeline));
    }

    // sort and reduce to distinct set of group ids here instead of in pipeline
    final TreeSet<String> groupIdsWithMultiplePairs = new TreeSet<>();

    // mongodb java 3.0 driver notes:
    // -- need to set cursor batchSize to prevent NPE from cursor creation
    final AggregateIterable<Document> iterable = matchCollection.aggregate(pipeline).batchSize(1);
    try (final MongoCursor<Document> cursor = iterable.iterator()) {
        while (cursor.hasNext()) {
            final Document id = cursor.next().get("_id", Document.class);
            groupIdsWithMultiplePairs.add(id.getString("pGroupId"));
            if (includeQGroupIds) {
                groupIdsWithMultiplePairs.add(id.getString("qGroupId"));
            }
        }
    }

    return groupIdsWithMultiplePairs;
}
 
Example #14
Source File: RenderDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
private Document getGroupQuery(final Double minZ,
                               final Double maxZ,
                               final String groupId,
                               final Double minX,
                               final Double maxX,
                               final Double minY,
                               final Double maxY)
        throws IllegalArgumentException {

    final Document groupQuery = new Document();

    if ((minZ != null) && minZ.equals(maxZ)) {
        groupQuery.append("z", minZ);
    } else {
        if (minZ != null) {
            if (maxZ != null) {
                groupQuery.append("z", new Document(QueryOperators.GTE, minZ).append(QueryOperators.LTE, maxZ));
            } else {
                groupQuery.append("z", new Document(QueryOperators.GTE, minZ));
            }
        } else if (maxZ != null) {
            groupQuery.append("z", new Document(QueryOperators.LTE, maxZ));
        }
    }

    if (groupId != null) {
        groupQuery.append("groupId", groupId);
    }

    if (minX != null) {
        groupQuery.append("maxX", new Document(QueryOperators.GTE, minX));
    }
    if (maxX != null) {
        groupQuery.append("minX", new Document(QueryOperators.LTE, maxX));
    }
    if (minY != null) {
        groupQuery.append("maxY", new Document(QueryOperators.GTE, minY));
    }
    if (maxY != null) {
        groupQuery.append("minY", new Document(QueryOperators.LTE, maxY));
    }

    return groupQuery;
}
 
Example #15
Source File: RenderDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
private Document gte(final double value) {
    return new Document(QueryOperators.GTE, value);
}
 
Example #16
Source File: RenderDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
private Document lte(final double value) {
    return new Document(QueryOperators.LTE, value);
}
 
Example #17
Source File: RenderDao.java    From render with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return list of section data objects for the specified stackId.
 *
 * @throws IllegalArgumentException
 *   if any required parameters are missing or the stack section data has not been built.
 *
 * @throws ObjectNotFoundException
 *   if the stack cannot be found.
 */
public List<SectionData> getSectionData(final StackId stackId,
                                        final Double minZ,
                                        final Double maxZ)
        throws IllegalArgumentException, ObjectNotFoundException {

    MongoUtil.validateRequiredParameter("stackId", stackId);

    final List<SectionData> list = new ArrayList<>();

    if (! MongoUtil.exists(renderDatabase, stackId.getSectionCollectionName())) {
        throwExceptionIfStackIsMissing(stackId);
        throw new IllegalArgumentException("section data not aggregated for " + stackId +
                                           ", set stack state to COMPLETE to generate the aggregate collection");
    }

    final MongoCollection<Document> sectionCollection = getSectionCollection(stackId);

    final Document query = new Document();
    if (minZ != null) {
        if (maxZ != null) {
            query.append("_id.z", new Document(QueryOperators.GTE, minZ).append(QueryOperators.LTE, maxZ));
        } else {
            query.append("_id.z", new Document(QueryOperators.GTE, minZ));
        }
    } else if (maxZ != null) {
        query.append("_id.z", new Document(QueryOperators.LTE, maxZ));
    }

    try (final MongoCursor<Document> cursor = sectionCollection.find(query).iterator()) {
        Document document;
        Document resultId;
        String sectionId;
        Number tileCount;
        Long tileCountLong = null;
        Double z;
        Double minX;
        Double maxX;
        Double minY;
        Double maxY;
        while (cursor.hasNext()) {
            document = cursor.next();
            resultId = document.get("_id", Document.class);
            sectionId = resultId.get("sectionId", String.class);
            z = resultId.get("z", Double.class);

            // Need to convert tileCount to long this way because aggregation seems
            // to create an Integer if the tileCount is small enough.
            // Using a long "just in case" there is a section with more than 2^31-1 tiles.
            tileCount = document.get("tileCount", Number.class);
            if (tileCount != null) {
                tileCountLong = tileCount.longValue();
            }

            minX = document.getDouble("minX");
            maxX = document.getDouble("maxX");
            minY = document.getDouble("minY");
            maxY = document.getDouble("maxY");

            if ((sectionId != null) && (z != null)) {
                list.add(new SectionData(sectionId, z, tileCountLong, minX, maxX, minY, maxY));
            }
        }
    }

    LOG.debug("getSectionData: returning {} values for {}.find({})",
              list.size(), sectionCollection.getNamespace().getFullName());

    return list;
}
 
Example #18
Source File: Support.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public ConstraintBuilder equal(String name, boolean negate, @Nullable Object value) {
  addContraint(name, negate ? new Document(QueryOperators.NE, unwrapBsonable(value)) : unwrapBsonable(value));
  return this;
}
 
Example #19
Source File: Support.java    From immutables with Apache License 2.0 4 votes vote down vote up
private Object negateConstraint(boolean negate, Object constraint) {
  return negate ? new Document(QueryOperators.NOT, constraint) : constraint;
}
 
Example #20
Source File: Support.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public ConstraintBuilder present(String name, boolean negate) {
  addContraint(name, new Document(QueryOperators.EXISTS, !negate));
  return this;
}
 
Example #21
Source File: QueryBuilder.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Equivalent of the $centerSphere operand mostly intended for queries up to a few hundred miles or km.
 *
 * @param longitude   coordinate in decimal degrees
 * @param latitude    coordinate in decimal degrees
 * @param maxDistance max spherical distance
 * @return {@code this}
 */
public QueryBuilder withinCenterSphere(final double longitude, final double latitude, final double maxDistance) {
    addOperand(QueryOperators.WITHIN,
               new Document(QueryOperators.CENTER_SPHERE,
                                 asList(asList(longitude, latitude), maxDistance)));
    return this;
}
 
Example #22
Source File: QueryBuilder.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Equivalent of the $nearSphere operand
 *
 * @param longitude   coordinate in decimal degrees
 * @param latitude    coordinate in decimal degrees
 * @param maxDistance max spherical distance
 * @return {@code this}
 */
public QueryBuilder nearSphere(final double longitude, final double latitude, final double maxDistance) {
    addOperand(QueryOperators.NEAR_SPHERE,
               asList(longitude, latitude));
    addOperand(QueryOperators.MAX_DISTANCE,
               maxDistance);
    return this;
}
 
Example #23
Source File: QueryBuilder.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Equivalent of the $near operand
 *
 * @param x           x coordinate
 * @param y           y coordinate
 * @param maxDistance max distance
 * @return {@code this}
 */
public QueryBuilder near(final double x, final double y, final double maxDistance) {
    addOperand(QueryOperators.NEAR,
               asList(x, y));
    addOperand(QueryOperators.MAX_DISTANCE,
               maxDistance);
    return this;
}
 
Example #24
Source File: Support.java    From immutables with Apache License 2.0 3 votes vote down vote up
public Document asDocument() {
  final List<Document> result = new ArrayList<>(disjunctions);
  result.add(mergeConjunctions(conjunctions));

  return result.size() == 1 ? result.get(0) : new Document(QueryOperators.OR, result);

}
 
Example #25
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent to the $gt operator
 *
 * @param object Value to query
 * @return {@code this}
 */
public QueryBuilder greaterThan(final Object object) {
    addOperand(QueryOperators.GT, object);
    return this;
}
 
Example #26
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent to a $within operand, based on a bounding box using represented by two corners
 *
 * @param x  the x coordinate of the first box corner.
 * @param y  the y coordinate of the first box corner.
 * @param x2 the x coordinate of the second box corner.
 * @param y2 the y coordinate of the second box corner.
 * @return {@code this}
 */
public QueryBuilder withinBox(final double x, final double y, final double x2, final double y2) {
    addOperand(QueryOperators.WITHIN,
               new Document(QueryOperators.BOX, new Object[]{new Double[]{x, y}, new Double[]{x2, y2}}));
    return this;
}
 
Example #27
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent to the $gte operator
 *
 * @param object Value to query
 * @return {@code this}
 */
public QueryBuilder greaterThanEquals(final Object object) {
    addOperand(QueryOperators.GTE, object);
    return this;
}
 
Example #28
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent to the $lt operand
 *
 * @param object Value to query
 * @return {@code this}
 */
public QueryBuilder lessThan(final Object object) {
    addOperand(QueryOperators.LT, object);
    return this;
}
 
Example #29
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent to the $lte operand
 *
 * @param object Value to query
 * @return {@code this}
 */
public QueryBuilder lessThanEquals(final Object object) {
    addOperand(QueryOperators.LTE, object);
    return this;
}
 
Example #30
Source File: QueryBuilder.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Equivalent of the $ne operand
 *
 * @param object Value to query
 * @return {@code this}
 */
public QueryBuilder notEquals(final Object object) {
    addOperand(QueryOperators.NE, object);
    return this;
}