MongoDB: cursor.readPref() method
cursor.readPref
The cursor.readPref() method is used to specifiy a read preference to a cursor to control how the client directs queries to members of the replica set.
Syntax:
cursor.readPref(mode, tagSet)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
mode | One of the following read preference modes: primary, primaryPreferred, secondary, secondaryPreferred, or nearest | Required | string |
tagSet | A tag set used to specify custom read preference modes. For details. | Optional | array |
NOTE:You must apply readPref() to the cursor before retrieving any documents from the database.
Sample document in the restaurants 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.readPref() method
db.restaurants.find({"cuisine" : "American "}).readPref();
Output:
> db.restaurants.find({"cuisine" : "American "}).limit(2).readPref(); { "_id" : ObjectId("55c3043ab165fa6355ec5c8c"), "address" : { "building" : "2780", "coord" : [ -73.98241999999999, 40.579505 ], "street" : "Stillwell Avenue", "zipcode" : "11224" }, "borough" : "Brooklyn", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-06-10T00:00:00Z"), "grade" : "A ", "score" : 5 }, { "date" : ISODate("2013-06-05T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-04-13T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-10-12T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Riviera Caterer", "restaurant_id" : "40356018" } { "_id" : ObjectId("55c3043ab165fa6355ec5c8e"), "address" : { "building" : "8825", "coord" : [ -73.8803827, 40.7643124 ], "street" : "Astoria Boulevard", "zipcode" : "11369" }, "borough" : "Queens", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-11-15T00:00:00Z"), "grade" : "Z", "sco re" : 38 }, { "date" : ISODate("2014-05-02T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2013-03-02T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-02-10T00:00:00Z"), "grade" : "A", "score" : 13 } ], "name" : "Brunos On The Boulevard", "restaurant_id" : "4035615 1" }
Retrieve the restaurants data from here
Previous:
cursor.pretty() method
Next:
cursor.showDiskLoc() 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-readPref.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics