MongoDB: cursor.hint() method
cursor.hint
The cursor.hint() method is used to force MongoDB to use a specific index for a query.
Syntax:
cursor.hint(index)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
index | The index to hint or force MongoDB to use when performing the query. Specify the index either by the index name or by the index specification document. | Required | string or document |
Sample document in the restaurants_new collection:
{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}
...............
Example: MongoDB: cursor.hint() method
Assume that the following index already exists in restaurants_new collection
> db.restaurants_new.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.restaurants_new"
},
{
"v" : 1,
"key" : {
"cuisine" : 1
},
"name" : "cuisine_1",
"ns" : "test.restaurants_new"
}
]
The following example returns all documents in the collection named restaurants_new using the index on the cuisine field.
db.restaurants_new.find().hint( { cuisine: 1 } );
or
db.restaurants_new.find().hint( "cuisine_1" );
Output:
> db.restaurants_new.find().hint( { cuisine: 1 } ); { "_id" : ObjectId("55c3043ab165fa6355ec6374"), "address" : { "building" : "1345", "coord" : [ -73.959249, 40.768076 ], "street" : "2 Avenue", "zipcode" : "10021" }, "borough" : "Manhattan", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-10-07T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2013-10-23T00:00:00Z"), "grade" : "A", "score" : 8 }, { "date" : ISODate("2012-10-26T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-04-26T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-01-12T00:00:00Z"), "grade" : "P", "score" : 10 } ], "name" : "Afghan Kebab House", "restaurant_id" : "40552806" } { "_id" : ObjectId("55c3043ab165fa6355ec649f"), "address" : { "building" : "34", "coord" : [ -73.9883612, 40.7286391 ], "street" : "St Marks Place", "zipcode" : "10003" }, "borough" : "Manhattan", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-02-20T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-06T00:00:00Z"), "grade" : "B", "score" : 17 }, { "date" : ISODate("2013-01-31T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-05-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-12-13T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-08-25T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Khyber Pass", "restaurant_id" : "40589545" } { "_id" : ObjectId("55c3043ab165fa6355ec6558"), "address" : { "building" : "764", "coord" : [ -73.9880435, 40.7641423 ], "street" : "9 Avenue", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-12-30T00:00:00Z"), "grade" : "A", "score" : 12 } , { "date" : ISODate("2013-10-10T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("2013-02-25T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-08-09T00:00:00Z"), "grade" : "C", "score" : 2 }, { "date" : ISODate("2012-02-16T00:00:00Z"), "grade" : "A", "score" : 9 } ] , "name" : "Afghan Kebab House #1", "restaurant_id" : "40616799" } { "_id" : ObjectId("55c3043ab165fa6355ec6a7b"), "address" : { "building" : "787", "coord" : [ -73.9880886, 40.7651331 ], "street" : "9 Avenue", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-07-22T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2013-07-08T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-02-05T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2011-11-28T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Ariana Kebab House", "restaurant_id" : "40868400" } { "_id" : ObjectId("55c3043bb165fa6355ec8ce2"), "address" : { "building" : "259-11", "coord" : [ -73.708831, 40.73748399999999 ], "street" : "Hillside Avenue", "zipcode" : "11004" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-09-15T00:00:00Z"), "grade" : "A", " score" : 13 }, { "date" : ISODate("2013-09-18T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-04-18T00:00:00Z"), "grade" : "B", "score" : 21 }, { "date" : ISODate("2012-08-29T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-02-09T00:00:00Z"), "grade" : "A", "sc ore" : 11 } ], "name" : "Afghan Kebob House", "restaurant_id" : "41535706" } { "_id" : ObjectId("55c3043bb165fa6355ec8f12"), "address" : { "building" : "68-16", "coord" : [ -73.79474420000001, 40.7332144 ], "street" : "Fresh Meadow Lane", "zipcode" : "11365" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2013-11-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-06-01T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-02-27T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-09-03T00:00:00Z"), "grade" : "C", "score" : 34 } ], "name" : "Bakhter Afghan Halal Kabab", "restaurant_id" : "41559771" } { "_id" : ObjectId("55c3043bb165fa6355ec900e"), "address" : { "building" : "43-27", "coord" : [ -73.8260645, 40.751886 ], "street" : "Main Street", "zipcode" : "11355" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-08-29T00:00:00Z"), "grade" : "B", "score" : 15 }, { "date" : ISODate("2013-03-05T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2013-02-13T00:00:00Z"), "grade" : "P", "score" : 5 }, { "date" : ISODate("2012-06-28T00:00:00Z"), "grade" : "B", "score" : 17 } , { "date" : ISODate("2011-06-18T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Choopan Kabab Restaurant", "restaurant_id" : "41569155" } { "_id" : ObjectId("55c3043bb165fa6355ec9b4e"), "address" : { "building" : "6729", "coord" : [ -73.8204147, 40.724243 ], "street" : "Main St", "zipcode" : "11367" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-08-20T00:00:00Z"), "grade" : "A", "score" : 4 }, { " date" : ISODate("2013-07-31T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-08-04T00:00:00Z"), "grade" : "A", "score" : 5 } ], "name" : "Bakhtar Kabab", "restaurant_id" : "41661199" } { "_id" : ObjectId("55c3043bb165fa6355ec9c32"), "address" : { "building" : "984", "coord" : [ -73.9673814, 40.63341399999999 ], "street" : "Coney Island Avenue", "zipcode" : "11230" }, "borough" : "Brooklyn", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-09-19T00:00:00Z"), "grade" : "C ", "score" : 38 }, { "date" : ISODate("2014-03-27T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-02-12T00:00:00Z"), "grade" : "P", "score" : 6 }, { "date" : ISODate("2013-06-18T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-06-05T00:00:00Z"), "grade" : "A" , "score" : 6 } ], "name" : "Bahar Masala", "restaurant_id" : "41670224" } { "_id" : ObjectId("55c3043bb165fa6355eca97a"), "address" : { "building" : "110-23", "coord" : [ -73.8374947, 40.696891 ], "street" : "Jamaica Avenue", "zipcode" : "11418" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-07-09T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2014-01-02T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "New Bkhatar Afghan Halal Kabab & Gyro King", "restaurant_id" : "50001906" } { "_id" : ObjectId("55c3043bb165fa6355ecb140"), "address" : { "building" : "2310", "coord" : [ -73.91336969999999, 40.7742674 ], "street" : "31St St", "zipcode" : "11105" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-03-27T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Balkh Shish Kabab House", "restaurant_id" : "50007432" } { "_id" : ObjectId("55c3043bb165fa6355ecb260"), "address" : { "building" : "33-35", "coord" : [ -73.7896131, 40.7666136 ], "street" : "Francis Lewis Boulevard", "zipcode" : "11358" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-06-25T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Arya Kabob House", "restaurant_id" : "50008452" } { "_id" : ObjectId("55c3043bb165fa6355ecb4d8"), "address" : { "building" : "72-55", "coord" : [ -73.812895, 40.7276946 ], "street" : "Kissena Boulevard", "zipcode" : "11367" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2014-08-28T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Tariq Afghan Kabab", "restaurant_id" : "50010806" } { "_id" : ObjectId("55c3043bb165fa6355ecb4da"), "address" : { "building" : "68-16", "coord" : [ -73.79474420000001, 40.7332144 ], "street" : "Fresh Meadow Lane", "zipcode" : "11365" }, "borough" : "Queens", "cuisine" : "Afghan", "grades" : [ { "date" : ISODate("2015-01-12T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2014-06-26T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Bakhter Afghan Halal Kabab", "restaurant_id" : "50010813" } { "_id" : ObjectId("55c3043ab165fa6355ec5d61"), "address" : { "building" : "2300", "coord" : [ -73.8786113, 40.8502883 ], "street" : "Southern Boulevard", "zipcode" : "10460" }, "borough" : "Bronx", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-05-21T00:00:00Z"), "grade" : "A", "score " : 8 }, { "date" : ISODate("2013-06-01T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2012-06-12T00:00:00Z"), "grade" : "A", "score" : 0 }, { "date" : ISODate("2011-06-08T00:00:00Z"), "grade" : "A", "score" : 0 } ], "name" : "African Terrace", "restaurant_id" : "40368021" } { "_id" : ObjectId("55c3043ab165fa6355ec667a"), "address" : { "building" : "195", "coord" : [ -73.9717845, 40.6897199 ], "street" : "Dekalb Avenue", "zipcode" : "11205" }, "borough" : "Brooklyn", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-08-18T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2014-01-13T00:00:00Z"), "grade" : "B", "score" : 19 }, { "date" : ISODate("2012-12-19T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-08-01T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-03-12T00:00:00Z"), "grade" : "A", "score" : 1 2 }, { "date" : ISODate("2011-11-01T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Madiba", "restaurant_id" : "40684161" } { "_id" : ObjectId("55c3043ab165fa6355ec68f0"), "address" : { "building" : "14612", "coord" : [ -73.8057078, 40.6948408 ], "street" : "Liberty Avenue", "zipcode" : "11435" }, "borough" : "Queens", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-04-16T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2013-09-16T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2012-08-23T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-02-21T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-06-30T00:00:00Z"), "grade" : "B", "score" : 19 } ], "name" : "Africana Restaurant", "restaurant_id" : "40798504" } { "_id" : ObjectId("55c3043ab165fa6355ec69a8"), "address" : { "building" : "2364", "coord" : [ -73.9025083, 40.8603246 ], "street" : "Jerome Avenue", "zipcode" : "10468" }, "borough" : "Bronx", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-04-22T00:00:00Z"), "grade" : "A", "score" : 1 2 }, { "date" : ISODate("2013-10-22T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-09-18T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-10-04T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-05-26T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Ebe Ye Yie African Restaurant", "restaurant_id" : "40832718" } { "_id" : ObjectId("55c3043ab165fa6355ec6bbb"), "address" : { "building" : "2535", "coord" : [ -73.9277649, 40.81103 ], "street" : "3 Avenue", "zipcode" : "10451" }, "borough" : "Bronx", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-06-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2013-05-03T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-08-10T00:00:00Z"), "grade" : "P", "score" : 3 }, { "date" : ISODate("2012-04-14T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-10-15T00:00:00Z"), "grade" : "A", "score" : 10 } ], " name" : "African Maquis La Plantation", "restaurant_id" : "40923014" } { "_id" : ObjectId("55c3043ab165fa6355ec6edd"), "address" : { "building" : "1150", "coord" : [ -73.91670189999999, 40.8323228 ], "street" : "Sherman Avenue", "zipcode" : "10456" }, "borough" : "Bronx", "cuisine" : "African", "grades" : [ { "date" : ISODate("2014-12-23T00:00:00Z"), "grade" : "A", "sc ore" : 12 }, { "date" : ISODate("2013-12-24T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2012-12-07T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-05-15T00:00:00Z"), "grade" : "B", "score" : 14 } ], "name" : "Halal Coffee Shop Restaurant", "restaurant_id" : "4 1012979" }
Retrieve the restaurants data from here
Behavior
When an index filter exists for the query shape, MongoDB ignores the hint().
You cannot use hint() if the query includes a $text query expression.
Previous:
cursor.hasNext() method
Next:
cursor.itcount() method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/shell-methods/cursor/cursor-hint.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics