進階查詢

介紹進階查詢的用法之前,我們要先新增一些資料到 MongoDB 資料庫中,我們接續之前的 PHP 範例程式,一次加入多筆資料到 demo 這個 collection 中:

// 要儲存的資料
$record2 = array(
  'firstName' => 'Abner',
  'lastName' => 'Wang',
  'roles' => array('developer', 'webmaster')
);
$record3 = array(
  'firstName' => 'Horace',
  'lastName' => 'Wang',
  'roles' => array('user')
);
$record4 = array(
  'firstName' => 'Karry',
  'lastName' => 'Li',
  'roles' => array('developer')
);

// 將多筆資料插入 demo 這個 collection 中
$cDemo->batchInsert(
    array($record2, $record3, $record4),
    array('continueOnError' => true)
);

batchInsert 這個 PHP 函數可以用來一次插入多筆 documents 至指定的 collection 中,第一個參數是 documents 的陣列,而第二個參數則是選項的陣列。

之前我們使用的 findOne 只會傳回第一筆符合條件的資料,如果我們需要列出多筆資料時,就要使用 find 函數:

// 設定查詢條件
$queryCondition = array(
  'lastName' => 'Wang'
);

// 查詢並列出所有符合條件的資料
$cursor = $cDemo->find($queryCondition);
foreach ($cursor as $doc) {
  print_r($doc);
}

這樣就會列出所有符合搜尋條件的資料:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 564e936bb2fd7f962e8b4567
        )

    [firstName] => G.T.
    [lastName] => Wang
    [roles] => Array
        (
            [0] => developer
            [1] => webmaster
        )

)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4567
        )

    [firstName] => Abner
    [lastName] => Wang
    [roles] => Array
        (
            [0] => developer
            [1] => webmaster
        )

)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4568
        )

    [firstName] => Horace
    [lastName] => Wang
    [roles] => Array
        (
            [0] => user
        )

)

這是比較複雜的查詢條件,列出 roleswebmasteruser 的人:

// 設定查詢條件
$queryCondition = array(
  'roles' => array('$in' => array('webmaster', 'user'))
);

// 查詢並列出所有符合條件的資料
$cursor = $cDemo->find($queryCondition);
foreach ($cursor as $doc) {
  print_r($doc);
}

輸出為

Array
(
    [_id] => MongoId Object
        (
            [$id] => 564e936bb2fd7f962e8b4567
        )

    [firstName] => G.T.
    [lastName] => Wang
    [roles] => Array
        (
            [0] => developer
            [1] => webmaster
        )

)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4567
        )

    [firstName] => Abner
    [lastName] => Wang
    [roles] => Array
        (
            [0] => developer
            [1] => webmaster
        )

)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4568
        )

    [firstName] => Horace
    [lastName] => Wang
    [roles] => Array
        (
            [0] => user
        )

)

也可以使用 JavaScript 來設定查詢的篩選條件:

// 使用 JavaScript 設定查詢條件
$js = "function() {
  return this.lastName == 'Li' || this.roles == 'user';
}";
$queryCondition = array('$where' => $js);

// 查詢並列出所有符合條件的資料
$cursor = $cDemo->find($queryCondition);
foreach ($cursor as $doc) {
  print_r($doc);
}

這樣會篩選出 lastNameLirolesuser 的人:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4568
        )

    [firstName] => Horace
    [lastName] => Wang
    [roles] => Array
        (
            [0] => user
        )

)
Array
(
    [_id] => MongoId Object
        (
            [$id] => 564ebc30b2fd7fa2158b4569
        )

    [firstName] => Karry
    [lastName] => Li
    [roles] => Array
        (
            [0] => developer
        )

)

其餘的範例可參考 find 的網頁。