PHP Velho Oeste 2024

MongoCursor::hint

(PECL mongo >=0.9.0)

MongoCursor::hintクエリについてのヒントをデータベースに与える

説明

public MongoCursor::hint ( mixed $index ) : MongoCursor

パラメータ

index

クエリで使用するインデックス。 文字列を渡した場合は、インデックス名として扱います。 配列あるいはオブジェクトを渡した場合は、それに基づいてインデックスを組み立てます (MongoCollection::ensureIndex() の最初の引数のようになります)。

返り値

このカーソルを返します。

エラー / 例外

このカーソルの反復処理が始まっている場合に MongoCursorException をスローします。

変更履歴

バージョン 説明
1.4.0

index 引数に、文字列でインデックス名を指定できるよういなりました。 これより前のバージョンでは、配列またはオブジェクトしか渡せませんでした。

add a note add a note

User Contributed Notes 1 note

up
1
mr dot bipinks at gmail dot com
8 years ago
For the series we’ll assume we have a collection named posts populated with 500 documents having the following structure:

{
    "_id": ObjectId("5146bb52d852470060001f4"),
    "comments": {
        "0": "This is the first comment",
        "1": "This is the second comment"
    },
    "post_likes": 40,
    "post_tags": {
        "0": "MongoDB",
        "1": "Tutorial",
        "2": "Indexing"
    },
    "post_text": "Hello Readers!! This is my post text",
    "post_type": "private",
    "user_name": "Mark Anthony"
}

The hint() method can be used to force MongoDB to use an index we specify and override the default selection and query optimization process. You can specify the field names used in the index as a argument as shown below:

<?php
// query to find posts with type public and 100 likes
// use hint() to force MongoDB to use the index we created
$cursor = $collection
   
->find(
        array(
           
"post_type" => "public",
           
"post_likes" => 100
       
)
    )
    ->
hint(
        array(
           
"post_type" => 1,
           
"post_likes" => 1
       
)
    );
?>

This ensures the query uses the compound index defined on the post_type and post_likes fields.
To Top