public class

QueryBuilder

extends Object
java.lang.Object
   ↳ com.mongodb.QueryBuilder

Class Overview

Utility for creating DBObject queries

Summary

Public Constructors
QueryBuilder()
Creates a builder with an empty query
Public Methods
QueryBuilder all(Object object)
Equivalent of the $all operand
QueryBuilder and(DBObject... ands)
Equivalent to an $and operand
QueryBuilder and(String key)
Equivalent to QueryBuilder.put(key).
QueryBuilder exists(Object object)
Equivalent of the $exists operand
DBObject get()
Creates a DBObject query to be used for the driver's find operations
QueryBuilder greaterThan(Object object)
Equivalent to the $gt operator
QueryBuilder greaterThanEquals(Object object)
Equivalent to the $gte operator
QueryBuilder in(Object object)
Equivalent of the $in operand
QueryBuilder is(Object object)
Equivalent of the find({key:value})
QueryBuilder lessThan(Object object)
Equivalent to the $lt operand
QueryBuilder lessThanEquals(Object object)
Equivalent to the $lte operand
QueryBuilder mod(Object object)
Equivalent of the $mod operand
QueryBuilder near(double x, double y, double maxDistance)
Equivalent of the $near operand
QueryBuilder near(double x, double y)
Equivalent of the $near operand
QueryBuilder nearSphere(double longitude, double latitude, double maxDistance)
Equivalent of the $nearSphere operand
QueryBuilder nearSphere(double longitude, double latitude)
Equivalent of the $nearSphere operand
QueryBuilder notEquals(Object object)
Equivalent of the $ne operand
QueryBuilder notIn(Object object)
Equivalent of the $nin operand
QueryBuilder or(DBObject... ors)
Equivalent to a $or operand
QueryBuilder put(String key)
Adds a new key to the query if not present yet.
QueryBuilder regex(Pattern regex)
Passes a regular expression for a query
QueryBuilder size(Object object)
Equivalent of the $size operand
static QueryBuilder start(String key)
Creates a new query with a document key
static QueryBuilder start()
returns a new QueryBuilder
QueryBuilder withinBox(double x, double y, double x2, double y2)
Equivalent to a $within operand, based on a bounding box using represented by two corners
QueryBuilder withinCenter(double x, double y, double radius)
Equivalent of the $within operand, used for geospatial operation
QueryBuilder withinCenterSphere(double longitude, double latitude, double maxDistance)
Equivalent of the $centerSphere operand mostly intended for queries up to a few hundred miles or km.
QueryBuilder withinPolygon(List<Double[]> points)
Equivalent to a $within operand, based on a bounding polygon represented by an array of points
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public QueryBuilder ()

Creates a builder with an empty query

Public Methods

public QueryBuilder all (Object object)

Equivalent of the $all operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "matches all array contents" query

public QueryBuilder and (DBObject... ands)

Equivalent to an $and operand

public QueryBuilder and (String key)

Equivalent to QueryBuilder.put(key). Intended for compound query chains to be more readable Example: QueryBuilder.start("a").greaterThan(1).and("b").lessThan(3)

Parameters
key MongoDB document key
Returns
  • Returns the current QueryBuilder with an appended key operand

public QueryBuilder exists (Object object)

Equivalent of the $exists operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended exists operator

public DBObject get ()

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

Returns
  • Returns a DBObject query instance
Throws
RuntimeException if a key does not have a matching operand

public QueryBuilder greaterThan (Object object)

Equivalent to the $gt operator

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "greater than" query

public QueryBuilder greaterThanEquals (Object object)

Equivalent to the $gte operator

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "greater than or equals" query

public QueryBuilder in (Object object)

Equivalent of the $in operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "in array" query

public QueryBuilder is (Object object)

Equivalent of the find({key:value})

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended equality query

public QueryBuilder lessThan (Object object)

Equivalent to the $lt operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "less than" query

public QueryBuilder lessThanEquals (Object object)

Equivalent to the $lte operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "less than or equals" query

public QueryBuilder mod (Object object)

Equivalent of the $mod operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended modulo query

public QueryBuilder near (double x, double y, double maxDistance)

Equivalent of the $near operand

Parameters
x x coordinate
y y coordinate
maxDistance max distance

public QueryBuilder near (double x, double y)

Equivalent of the $near operand

Parameters
x x coordinate
y y coordinate

public QueryBuilder nearSphere (double longitude, double latitude, double maxDistance)

Equivalent of the $nearSphere operand

Parameters
longitude coordinate in decimal degrees
latitude coordinate in decimal degrees
maxDistance max spherical distance

public QueryBuilder nearSphere (double longitude, double latitude)

Equivalent of the $nearSphere operand

Parameters
longitude coordinate in decimal degrees
latitude coordinate in decimal degrees

public QueryBuilder notEquals (Object object)

Equivalent of the $ne operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended inequality query

public QueryBuilder notIn (Object object)

Equivalent of the $nin operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended "not in array" query

public QueryBuilder or (DBObject... ors)

Equivalent to a $or operand

public QueryBuilder put (String key)

Adds a new key to the query if not present yet. Sets this key as the current key.

Parameters
key MongoDB document key
Returns
  • Returns the current QueryBuilder

public QueryBuilder regex (Pattern regex)

Passes a regular expression for a query

Parameters
regex Regex pattern object
Returns
  • Returns the current QueryBuilder with an appended regex query

public QueryBuilder size (Object object)

Equivalent of the $size operand

Parameters
object Value to query
Returns
  • Returns the current QueryBuilder with an appended size operator

public static QueryBuilder start (String key)

Creates a new query with a document key

Parameters
key MongoDB document key
Returns
  • Returns a new QueryBuilder

public static QueryBuilder start ()

returns a new QueryBuilder

public QueryBuilder withinBox (double x, double y, double x2, double y2)

Equivalent to a $within operand, based on a bounding box using represented by two corners

Parameters
x the x coordinate of the first box corner.
y the y coordinate of the first box corner.
x2 the x coordinate of the second box corner.
y2 the y coordinate of the second box corner.

public QueryBuilder withinCenter (double x, double y, double radius)

Equivalent of the $within operand, used for geospatial operation

Parameters
x x coordinate
y y coordinate
radius radius

public QueryBuilder withinCenterSphere (double longitude, double latitude, double maxDistance)

Equivalent of the $centerSphere operand mostly intended for queries up to a few hundred miles or km.

Parameters
longitude coordinate in decimal degrees
latitude coordinate in decimal degrees
maxDistance max spherical distance

public QueryBuilder withinPolygon (List<Double[]> points)

Equivalent to a $within operand, based on a bounding polygon represented by an array of points

Parameters
points an array of Double[] defining the vertices of the search area