Where developers come to connect, share, build and be inspired.


PHP - MongoDB Advanced Queries


MongoDB offers a rich query environment with lots of features. This page lists some of those features.

Conditional Operators

<, <=, >, >=

Use these special forms for greater than and less than comparisons in queries, since they have to be represented in the query document:

Less than ...

    'size' => ['$lt' => 13]

Less than or equal to ...

    'size' => ['$lte' => 13]

Greater than ...

    'size' => ['$gt' => 13]

Greater than or equal to ...

    'size' => ['$gte' => 13]


The $all operator is similar to $in, but instead of matching any value in the specified array all values in the array must be matched.

    'size' => ['$all' => [1, 2, 3]]


Check for existence (or lack thereof) of a field.

    'size' => ['$exists' => TRUE]


The $mod operator allows you to do fast modulo queries to replace a common case for where clauses.

    'size' => ['$mod' => [2, 0]]

    'size' => ['$mod' => [2, 1]]


Use $ne for not equals.

    'size' => ['$ne' => 13]


The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.

    'size' => ['$in' => [1, 2, 3]]


The $nin operator is similar to $in except that it selects objects for which the specified field does not have any value in the specified array.

    'size' => ['$nin' => [1, 2, 3]]


The $or operator lets you use boolean or in a query. You give $or an array of expressions, any of which can satisfy the query.

    '$or' => [
        ['username' => 'kristories'],
        ['email'    => 'w.kristories@gmail.com']


The $and operator lets you use boolean and in a query. You give $and an array of expressions, all of which must match to satisfy the query.

    '$and' => [
        ['username' => 'kristories'],
        ['email'    => 'w.kristories@gmail.com']


The $size operator matches any array with the specified number of elements.

    'size' => ['$size' => 2]


The $type operator matches values based on their BSON type.

    'size' => ['$type' => 16]

Possible types are:

Type Name                   Type Number
Double                      1
String                      2
Object                      3
Array                       4
Binary data                 5
Object id                   7
Boolean                     8
Date                        9
Null                        10
Regular expression          11
JavaScript code             13
Symbol                      14
JavaScript code with scope  15
32-bit integer              16
Timestamp                   17
64-bit integer              18
Min key                     255*
Max key                     127

For more information on types and BSON in general, see http://www.bsonspec.org.

Cursor Methods


The count() method returns the number of objects matching the query specified.


# count(TRUE)
# Send cursor limit and skip information to the count



limit() is analogous to the LIMIT statement in MySQL - it specifies a maximum number of results to return.



The skip() expression allows one to specify at which object the database should begin returning results. This is often useful for implementing paging.



sort() is analogous to the ORDER BY statement in SQL - it requests that items be returned in a particular order.

# 1 for ascending sort, or -1 for descending sort.
$cursor->sort(['field' => 1]);


Add a comment