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

28

PHP - MongoDB Advanced Queries

1940 views

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 ...

$collection->find([
    'size' => ['$lt' => 13]
]);

Less than or equal to ...

$collection->find([
    'size' => ['$lte' => 13]
]);

Greater than ...

$collection->find([
    'size' => ['$gt' => 13]
]);

Greater than or equal to ...

$collection->find([
    'size' => ['$gte' => 13]
]);

$all

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.

$collection->find([
    'size' => ['$all' => [1, 2, 3]]
]);

$exists

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

$collection->find([
    'size' => ['$exists' => TRUE]
]);

$mod

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

$collection->find([
    'size' => ['$mod' => [2, 0]]
]);

$collection->find([
    'size' => ['$mod' => [2, 1]]
]);

$ne

Use $ne for not equals.

$collection->find([
    'size' => ['$ne' => 13]
]);

$in

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

$collection->find([
    'size' => ['$in' => [1, 2, 3]]
]);

$nin

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.

$collection->find([
    'size' => ['$nin' => [1, 2, 3]]
]);

$or

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.

$collection->find([
    '$or' => [
        ['username' => 'kristories'],
        ['email'    => 'w.kristories@gmail.com']
    ]
]);

$and

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.

$collection->find([
    '$and' => [
        ['username' => 'kristories'],
        ['email'    => 'w.kristories@gmail.com']
    ]
]);

$size

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

$collection->find([
    'size' => ['$size' => 2]
]);

$type

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

$collection->find([
    '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

count()

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

$cursor->count();

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

$cursor->count(TRUE);

limit()

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

$cursor->limit(10);

skip()

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

$cursor->skip(10);

sort()

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]);

Comments

Add a comment