Filters

The Filters class provides static factory methods for all the MongoDB query operators. Each method returns an instance of the Bson type, which can in turn be passed to any method that expects a query filter.

For brevity, you may choose to import the methods of the Filters class statically:

import org.mongodb.scala.model.Filters._

All the examples below assume this static import.

Comparison

The comparison operator methods include:

  • eq: Matches values that are equal to a specified value. Aliased to equal as eq is a reserved word.
  • gt: Matches values that are greater than a specified value.
  • gte: Matches values that are greater than or equal to a specified value.
  • lt: Matches values that are less than a specified value.
  • lte: Matches values that are less than or equal to a specified value.
  • ne: Matches all values that are not equal to a specified value. Aliased to notEqual as neq is a reserved word.
  • in: Matches any of the values specified in an array.
  • nin: Matches none of the values specified in an array.

Examples

This example creates a filter that selects all documents where the value of the qty field equals 20:

`eq`("qty", 20)
equal("qty", 20)

which will render as:

{  
   "qty" : 20
}

This example creates a filter that selects all documents where the value of the qty field is either 5 or 20:

in("qty", 5, 15)

Logical

The logical operator methods include:

  • and: Joins filters with a logical AND and selects all documents that match the conditions of both filters.
  • or: Joins filters with a logical OR and selects all documents that match the conditions of either filters.
  • not: Inverts the effect of a query expression and selects documents that do not match the filter.
  • nor: Joins filters with a logical NOR and selects all documents that fail to match both filters.

Examples

This example creates a filter that selects all documents where ther value of the qty field is greater than 20 and the value of the user field equals "jdoe":

and(gt("qty", 20), equal("user", "jdoe"))

The and method generates a $and operator only if necessary, as the query language implicity ands together all the elements in a filter. So the above example will render as:

{ 
   "qty" : { "$gt" : 20 },
   "user" : "jdoe"
}

This example creates a filter that selects all documents where the price field value equals 0.99 or 1.99; and the sale field value is equal to true or the qty field value is less than 20:

and(or(equal("price", 0.99), equal("price", 1.99)
    or(equal("sale", true), lt("qty", 20)))

This query cannot be constructed using an implicit and operation, because it uses the $or operator more than once. So it will render as:

{
 "$and" : 
    [
      { "$or" : [ { "price" : 0.99 }, { "price" : 1.99 } ] },
      { "$or" : [ { "sale" : true }, { "qty" : { "$lt" : 20 } } ] }
    ]
}

Arrays

The array operator methods include:

  • all: Matches arrays that contain all elements specified in the query
  • elemMatch: Selects documents if element in the array field matches all the specified $elemMatch conditions
  • size: Selects documents if the array field is a specified size

Examples

This example selects documents with a tags array containing both "ssl" and "security":

all("tags", "ssl", "security")

Elements

The elements operator methods include:

  • exists: Selects documents that have the specified field.
  • type: Selects documents if a field is of the specified type. Aliased to bsonType as type is a reserved word.

Examples

This example selects documents that have a qty field and its value does not equal 5 or 15:

and(exists("qty"), nin("qty", 5, 15))

Evaluation

The evaluation operator methods include:

  • mod: Performs a modulo operation on the value of a field and selects documents with a specified result.
  • regex: Selects documents where values match a specified regular expression.
  • text: Selects documemts matching a full-text search expression.
  • where: Matches documents that satisfy a JavaScript expression.

Examples

This example assumes a collection that has a text index in the field abstract. It selects documents that have a abstract field containing the term coffee:

text("abstract", "coffee")

Geospatial

The geospatial operator methods include:

  • geoWithin: Selects all documents containing a field whose value is a GeoJSON geometry that falls within within a bounding GeoJSON geometry.
  • geoWithinBox: Selects all documents containing a field with grid coordinates data that exist entirely within the specified box.
  • geoWithinPolygon: Selects all documents containing a field with grid coordinates data that exist entirely within the specified polygon.
  • geoWithinCenter: Selects all documents containing a field with grid coordinates data that exist entirely within the specified circle.
  • geoWithinCenterSphere: Selects geometries containing a field with geospatial data (GeoJSON or legacy coordinate pairs) that exist entirely within the specified circle, using spherical geometry.
  • geoIntersects: Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
  • near: Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
  • nearSphere: Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

To make it easier to construct GeoJSON-based filters, the driver also include a full GeoJSON class hierarchy:

  • Point: A representation of a GeoJSON Point.
  • MultiPoint: A representation of a GeoJSON MultiPoint.
  • LineString: A representation of a GeoJSON LineString.
  • MultiLineString: A representation of a GeoJSON MultiLineString.
  • Polygon: A representation of a GeoJSON Polygon.
  • MultiPolygon: A representation of a GeoJSON MultiPolygon.
  • GeometryCollection: A representation of a GeoJSON GeometryCollection.

Examples

This example creates a filter that selects all documents where the geo field contains a GeoJSON Geometry object that falls within the given polygon:

    val polygon: Polygon = Polygon(Seq(Position(0, 0), Position(4, 0),
                                       Position(4, 4), Position(0, 4),
                                       Position(0, 0)))
    geoWithin("geo", polygon)

Similarly, this example creates a filter that selects all documents where the geo field contains a GeoJSON Geometry object that intersects the given Point:

geoIntersects("geo", Point(Position(4, 0)))