Class Filters


  • public final class Filters
    extends Object
    Defines helper methods to generate filter operations for queries.
    Since:
    2.0
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static Filter all​(String field, Object val)
      Matches arrays that contain all elements specified in the query.
      static Filter and​(Filter... filters)
      Applies $and to a set of filters
      static Filter bitsAllClear​(String field, int bitMask)
      Matches numeric or binary values in which a set of bit positions all have a value of 0.
      static Filter bitsAllClear​(String field, int[] positions)
      Matches numeric or binary values in which a set of bit positions all have a value of 0.
      static Filter bitsAllSet​(String field, int bitMask)
      Matches numeric or binary values in which a set of bit positions all have a value of 1.
      static Filter bitsAllSet​(String field, int[] positions)
      Matches numeric or binary values in which a set of bit positions all have a value of 1.
      static Filter bitsAnyClear​(String field, Object val)
      Matches numeric or binary values in which any bit from a set of bit positions has a value of 0.
      static Filter bitsAnySet​(String field, Object val)
      Matches numeric or binary values in which any bit from a set of bit positions has a value of 1.
      static Filter box​(String field, com.mongodb.client.model.geojson.Point bottomLeft, com.mongodb.client.model.geojson.Point upperRight)
      Specifies a rectangular box using legacy coordinate pairs for $geoWithin queries.
      static Filter center​(String field, com.mongodb.client.model.geojson.Point center, double radius)
      Specifies a circle using legacy coordinate pairs to $geoWithin queries when using planar geometry.
      static Filter centerSphere​(String field, com.mongodb.client.model.geojson.Point center, double radius)
      Defines a circle for a geospatial query that uses spherical geometry.
      static Filter comment​(String field, Object val)
      Adds a comment to a query predicate.
      static Filter elemMatch​(String field, Filter... filters)
      Selects documents if element in the array field matches all the specified $elemMatch conditions.
      static Filter eq​(String field, Object val)
      Specifies equality condition.
      static Filter exists​(String field)
      Matches documents that have the specified field.
      static Filter expr​(Expression expression)
      Allows use of aggregation expressions within the query language.
      static Filter geoIntersects​(String field, com.mongodb.client.model.geojson.Geometry val)
      Selects geometries that intersect with a GeoJSON geometry.
      static Filter geometry​(String field, Object val)
      Specifies a geometry in GeoJSON format to geospatial query operators.
      static GeoWithinFilter geoWithin​(String field, com.mongodb.client.model.geojson.MultiPolygon polygon)
      Selects geometries within a bounding GeoJSON geometry.
      static GeoWithinFilter geoWithin​(String field, com.mongodb.client.model.geojson.Polygon polygon)
      Selects geometries within a bounding GeoJSON geometry.
      static Filter gt​(String field, Object val)
      $gt selects those documents where the value of the field is greater than the specified value.
      static Filter gte​(String field, Object val)
      $gte selects the documents where the value of the field is greater than or equal to a specified value (e.g.
      static Filter in​(String field, Iterable<?> val)
      The $in operator selects the documents where the value of a field equals any value in the specified array.
      static Filter jsonSchema​(String field, Object val)
      Deprecated, for removal: This API element is subject to removal in a future version.
      static Filter jsonSchema​(org.bson.Document schema)
      Filters documents against the given JSON Schema.
      static Filter lt​(String field, Object val)
      $lt selects the documents where the value of the field is less than the specified value.
      static Filter lte​(String field, Object val)
      $lte selects the documents where the value of the field is less than or equal to the specified value.
      static Filter maxDistance​(String field, Object val)
      Specifies a maximum distance to limit the results of $near and $nearSphere queries.
      static Filter minDistance​(String field, Object val)
      Specifies a minimum distance to limit the results of $near and $nearSphere queries.
      static Filter mod​(String field, long divisor, long remainder)
      Performs a modulo operation on the value of a field and selects documents with a specified result.
      static Filter ne​(String field, Object val)
      $ne selects the documents where the value of the field is not equal to the specified value.
      static NearFilter near​(String field, com.mongodb.client.model.geojson.Point point)
      Specifies a point for which a geospatial query returns the documents from nearest to farthest.
      static NearFilter nearSphere​(String field, com.mongodb.client.model.geojson.Point point)
      Returns geospatial objects in proximity to a point on a sphere.
      static Filter nin​(String field, Object val)
      $nin selects the documents where: the field value is not in the specified array or the field does not exist.
      static Filter nor​(Filter... filters)
      Applies $nor to a set of filters
      static Filter or​(Filter... filters)
      Applies $or to a set of filters
      static Filter polygon​(String field, com.mongodb.client.model.geojson.Point... points)
      Specifies a polygon to using legacy coordinate pairs for $geoWithin queries.
      static RegexFilter regex​(String field)
      Selects documents where values match a specified regular expression.
      static Filter size​(String field, int size)
      Selects documents if the array field is a specified size.
      static TextSearchFilter text​(String textSearch)
      Performs text search.
      static Filter type​(String field, Type val)
      Selects documents if a field is of the specified type.
      static Filter uniqueDocs​(String field, Object val)
      Deprecated.
      static Filter where​(String val)
      Matches documents that satisfy a JavaScript expression.
    • Method Detail

      • eq

        public static Filter eq​(String field,
                                Object val)
        Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $eq
      • exists

        public static Filter exists​(String field)
        Matches documents that have the specified field.
        Parameters:
        field - the field to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $exists
      • type

        public static Filter type​(String field,
                                  Type val)
        Selects documents if a field is of the specified type.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $type
      • expr

        public static Filter expr​(Expression expression)
        Allows use of aggregation expressions within the query language.
        Parameters:
        expression - the expression to evaluate
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $expr
      • jsonSchema

        public static Filter jsonSchema​(org.bson.Document schema)
        Filters documents against the given JSON Schema.
        Parameters:
        schema - the schema to use
        Returns:
        the filter
        Since:
        2.1
        MongoDB documentation
        Query Filter: $jsonSchema
      • mod

        public static Filter mod​(String field,
                                 long divisor,
                                 long remainder)
        Performs a modulo operation on the value of a field and selects documents with a specified result.
        Parameters:
        field - the field to check
        divisor - the value to divide by
        remainder - the remainder to check for
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $mod
      • regex

        public static RegexFilter regex​(String field)
        Selects documents where values match a specified regular expression.
        Parameters:
        field - the field to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $regex
      • where

        public static Filter where​(String val)
        Matches documents that satisfy a JavaScript expression.
        Parameters:
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $where
      • geoIntersects

        public static Filter geoIntersects​(String field,
                                           com.mongodb.client.model.geojson.Geometry val)
        Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $geoIntersects
      • near

        public static NearFilter near​(String field,
                                      com.mongodb.client.model.geojson.Point point)
        Specifies a point for which a geospatial query returns the documents from nearest to farthest. The $near operator can specify either a GeoJSON point or legacy coordinate point.

        This requires a geospatial index.

        Parameters:
        field - the field to check
        point - the point to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $near
      • geoWithin

        public static GeoWithinFilter geoWithin​(String field,
                                                com.mongodb.client.model.geojson.Polygon polygon)
        Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
        Parameters:
        field - the field to check
        polygon - the polygon to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $geoWithin
      • geoWithin

        public static GeoWithinFilter geoWithin​(String field,
                                                com.mongodb.client.model.geojson.MultiPolygon polygon)
        Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
        Parameters:
        field - the field to check
        polygon - the polygon to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $geoWithin
      • nearSphere

        public static NearFilter nearSphere​(String field,
                                            com.mongodb.client.model.geojson.Point point)
        Returns geospatial objects in proximity to a point on a sphere.

        Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

        Parameters:
        field - the field to check
        point - the point to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $nearSphere
      • all

        public static Filter all​(String field,
                                 Object val)
        Matches arrays that contain all elements specified in the query.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $all
      • elemMatch

        public static Filter elemMatch​(String field,
                                       Filter... filters)
        Selects documents if element in the array field matches all the specified $elemMatch conditions.
        Parameters:
        field - the field to check
        filters - the filters to evaluate against
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $elemMatch
      • size

        public static Filter size​(String field,
                                  int size)
        Selects documents if the array field is a specified size.
        Parameters:
        field - the field to check
        size - the size to check against
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $size
      • bitsAllClear

        public static Filter bitsAllClear​(String field,
                                          int[] positions)
        Matches numeric or binary values in which a set of bit positions all have a value of 0.
        Parameters:
        field - the field to check
        positions - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAllClear
      • bitsAllClear

        public static Filter bitsAllClear​(String field,
                                          int bitMask)
        Matches numeric or binary values in which a set of bit positions all have a value of 0.
        Parameters:
        field - the field to check
        bitMask - the numeric bitmask to use
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAllClear
      • bitsAllSet

        public static Filter bitsAllSet​(String field,
                                        int bitMask)
        Matches numeric or binary values in which a set of bit positions all have a value of 1.
        Parameters:
        field - the field to check
        bitMask - the numeric bitmask to use
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAllSet
      • bitsAllSet

        public static Filter bitsAllSet​(String field,
                                        int[] positions)
        Matches numeric or binary values in which a set of bit positions all have a value of 1.
        Parameters:
        field - the field to check
        positions - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAllSet
      • bitsAnyClear

        public static Filter bitsAnyClear​(String field,
                                          Object val)
        Matches numeric or binary values in which any bit from a set of bit positions has a value of 0.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAnyClear
      • bitsAnySet

        public static Filter bitsAnySet​(String field,
                                        Object val)
        Matches numeric or binary values in which any bit from a set of bit positions has a value of 1.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $bitsAnySet
      • comment

        public static Filter comment​(String field,
                                     Object val)
        Adds a comment to a query predicate.
        Parameters:
        field - the field to check
        val - the value to check
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $comment
      • box

        public static Filter box​(String field,
                                 com.mongodb.client.model.geojson.Point bottomLeft,
                                 com.mongodb.client.model.geojson.Point upperRight)
        Specifies a rectangular box using legacy coordinate pairs for $geoWithin queries. The 2d index supports $box.
        Parameters:
        field - the field to check
        bottomLeft - the bottom left corner of the box
        upperRight - the upper right corner of the box
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $box
      • center

        public static Filter center​(String field,
                                    com.mongodb.client.model.geojson.Point center,
                                    double radius)
        Specifies a circle using legacy coordinate pairs to $geoWithin queries when using planar geometry. The 2d index supports $center.
        Parameters:
        field - the field to check
        center - the center point of the shape
        radius - the radius of the circle
        Returns:
        the filter
        MongoDB documentation
        Query Filter: $center
      • centerSphere

        public static Filter centerSphere​(String field,
                                          com.mongodb.client.model.geojson.Point center,
                                          double radius)
        Defines a circle for a geospatial query that uses spherical geometry. The query returns documents that are within the bounds of the circle. You can use the $centerSphere operator on both GeoJSON objects and legacy coordinate pairs.

        To use $centerSphere, specify an array that contains:

      • The grid coordinates of the circle’s center point, and
      • The circle’s radius measured in radians. To calculate radians, see Calculate Distance Using Spherical Geometry.
Parameters:
field - the field to check
center - the center point of the shape
radius - the radius of the circle
Returns:
the filter
MongoDB documentation
Query Filter: $centerSphere
  • geometry

    public static Filter geometry​(String field,
                                  Object val)
    Specifies a geometry in GeoJSON format to geospatial query operators.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $geometry
  • maxDistance

    public static Filter maxDistance​(String field,
                                     Object val)
    Specifies a maximum distance to limit the results of $near and $nearSphere queries. The 2dsphere and 2d indexes support $maxDistance.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $maxDistance
  • minDistance

    public static Filter minDistance​(String field,
                                     Object val)
    Specifies a minimum distance to limit the results of $near and $nearSphere queries. For use with 2dsphere index only.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $minDistance
  • polygon

    public static Filter polygon​(String field,
                                 com.mongodb.client.model.geojson.Point... points)
    Specifies a polygon to using legacy coordinate pairs for $geoWithin queries. The 2d index supports $center.
    Parameters:
    field - the field to check
    points - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $polygon
  • uniqueDocs

    public static Filter uniqueDocs​(String field,
                                    Object val)
    Deprecated. Modifies a $geoWithin and $near queries to ensure that even if a document matches the query multiple times, the query returns the document once.}
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $uniqueDocs
  • gt

    public static Filter gt​(String field,
                            Object val)
    $gt selects those documents where the value of the field is greater than the specified value.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $gt
  • gte

    public static Filter gte​(String field,
                             Object val)
    $gte selects the documents where the value of the field is greater than or equal to a specified value (e.g. value.)
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $gte
  • in

    public static Filter in​(String field,
                            Iterable<?> val)
    The $in operator selects the documents where the value of a field equals any value in the specified array.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $in
  • lt

    public static Filter lt​(String field,
                            Object val)
    $lt selects the documents where the value of the field is less than the specified value.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $lt
  • lte

    public static Filter lte​(String field,
                             Object val)
    $lte selects the documents where the value of the field is less than or equal to the specified value.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $lte
  • ne

    public static Filter ne​(String field,
                            Object val)
    $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $ne
  • nin

    public static Filter nin​(String field,
                             Object val)
    $nin selects the documents where:
    • the field value is not in the specified array or
    • the field does not exist.
    Parameters:
    field - the field to check
    val - the value to check
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $nin
  • nor

    public static Filter nor​(Filter... filters)
    Applies $nor to a set of filters
    Parameters:
    filters - the filters
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $nor
  • or

    public static Filter or​(Filter... filters)
    Applies $or to a set of filters
    Parameters:
    filters - the filters
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $or
  • and

    public static Filter and​(Filter... filters)
    Applies $and to a set of filters
    Parameters:
    filters - the filters
    Returns:
    the filter
    MongoDB documentation
    Query Filter: $and