MongoCursor::hint
(PECL mongo >=0.9.0)
MongoCursor::hint — Gives the database a hint about the query
Description
Parameters
-
index
-
Index to use for the query. If a string is passed, it should correspond to an index name. If an array or object is passed, it should correspond to the specification used to create the index (i.e. the first argument to MongoCollection::ensureIndex()).
Return Values
Returns this cursor.
Errors/Exceptions
Throws MongoCursorException if this cursor has started iterating.
Changelog
Version | Description |
---|---|
1.4.0 |
The |
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Базовые классы
- Функция MongoCursor::addOption() - Adds a top-level key/value pair to a query
- Функция MongoCursor::awaitData() - Sets whether this cursor will wait for a while for a tailable cursor to return more data
- Функция MongoCursor::batchSize() - Limits the number of elements returned in one batch.
- Функция MongoCursor::__construct() - Create a new cursor
- Функция MongoCursor::count() - Counts the number of results for this query
- Функция MongoCursor::current() - Returns the current element
- Функция MongoCursor::dead() - Checks if there are documents that have not been sent yet from the database for this cursor
- Функция MongoCursor::doQuery() - Execute the query.
- Функция MongoCursor::explain() - Return an explanation of the query, often useful for optimization and debugging
- Функция MongoCursor::fields() - Sets the fields for a query
- Функция MongoCursor::getNext() - Return the next object to which this cursor points, and advance the cursor
- Функция MongoCursor::getReadPreference() - Get the read preference for this query
- Функция MongoCursor::hasNext() - Checks if there are any more elements in this cursor
- Функция MongoCursor::hint() - Gives the database a hint about the query
- Функция MongoCursor::immortal() - Sets whether this cursor will timeout
- Функция MongoCursor::info() - Gets the query, fields, limit, and skip for this cursor
- Функция MongoCursor::key() - Returns the current result's _id
- Функция MongoCursor::limit() - Limits the number of results returned
- Функция MongoCursor::maxTimeMS() - Sets a server-side timeout for this query
- Функция MongoCursor::next() - Advances the cursor to the next result
- Функция MongoCursor::partial() - If this query should fetch partial results from mongos if a shard is down
- Функция MongoCursor::reset() - Clears the cursor
- Функция MongoCursor::rewind() - Returns the cursor to the beginning of the result set
- Функция MongoCursor::setFlag() - Sets arbitrary flags in case there is no method available the specific flag
- Функция MongoCursor::setReadPreference() - Set the read preference for this query
- Функция MongoCursor::skip() - Skips a number of results
- Функция MongoCursor::slaveOkay() - Sets whether this query can be done on a secondary [deprecated]
- Функция MongoCursor::snapshot() - Use snapshot mode for the query
- Функция MongoCursor::sort() - Sorts the results by given fields
- Функция MongoCursor::tailable() - Sets whether this cursor will be left open after fetching the last results
- Функция MongoCursor::timeout() - Sets a client-side timeout for this query
- Функция MongoCursor::valid() - Checks if the cursor is reading a valid result.
Коментарии
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.