{"action":"opened","number":1042,"pull_request":{"url":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042","id":38519228,"html_url":"https://github.com/MongoEngine/mongoengine/pull/1042","diff_url":"https://github.com/MongoEngine/mongoengine/pull/1042.diff","patch_url":"https://github.com/MongoEngine/mongoengine/pull/1042.patch","issue_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/1042","number":1042,"state":"open","locked":false,"title":"Fix read_preference","user":{"login":"wojcikstefan","id":1718372,"avatar_url":"https://avatars.githubusercontent.com/u/1718372?v=3","gravatar_id":"","url":"https://api.github.com/users/wojcikstefan","html_url":"https://github.com/wojcikstefan","followers_url":"https://api.github.com/users/wojcikstefan/followers","following_url":"https://api.github.com/users/wojcikstefan/following{/other_user}","gists_url":"https://api.github.com/users/wojcikstefan/gists{/gist_id}","starred_url":"https://api.github.com/users/wojcikstefan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wojcikstefan/subscriptions","organizations_url":"https://api.github.com/users/wojcikstefan/orgs","repos_url":"https://api.github.com/users/wojcikstefan/repos","events_url":"https://api.github.com/users/wojcikstefan/events{/privacy}","received_events_url":"https://api.github.com/users/wojcikstefan/received_events","type":"User","site_admin":false},"body":"I noticed something disturbing in our query logs earlier today - some of the queries were incorrectly routed to the primary when using `ReadPreference.SECONDARY`. Eventually, it came to my realization that `read_preference` wasn't working if it was put after `limit`, `skip`, or `hint` (and probably some other methods that allow chaining).\r\n\r\nNote: As of now, the fix only works for pymongo ver < 3.0. I think `read_preference` is **completely** broken with ver >= 3.0, but I don't have full clarity on the situation yet. It seems that `self._collection.find` doesn't accept `read_preference` as a valid kwarg any more and if I'm reading pymongo's docs right, you can only set read preference on a client/database/collection, not a particular cursor. It doesn't seem to be set either way:\r\n\r\n```\r\nIn [10]: import mongoengine\r\n\r\nIn [11]: from mongoengine import *\r\n\r\nIn [12]: from pymongo.read_preferences import ReadPreference\r\n\r\nIn [13]: import pymongo\r\n\r\nIn [14]: pymongo.version\r\nOut[14]: '3.0.2'\r\n\r\nIn [15]: class A(Document):\r\n    txt = StringField()\r\n   ....:\r\n\r\nIn [16]: qs = A.objects.read_preference(ReadPreference.SECONDARY)\r\n\r\nIn [17]: qs._cursor._Cursor__read_preference\r\nOut[17]: Primary()\r\n\r\nIn [18]: qs._cursor.collection.read_preference\r\nOut[18]: Primary()\r\n```","created_at":"2015-06-25T07:32:00Z","updated_at":"2015-06-25T07:32:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2c757aa4d752bf883276c05a481b18eff527adb0","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042/commits","review_comments_url":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042/comments","review_comment_url":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/comments{/number}","comments_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/1042/comments","statuses_url":"https://api.github.com/repos/MongoEngine/mongoengine/statuses/3f803372f8d3bdf8623a98b2945f8b76762a93d7","head":{"label":"elasticsales:fix-read-preference","ref":"fix-read-preference","sha":"3f803372f8d3bdf8623a98b2945f8b76762a93d7","user":{"login":"elasticsales","id":437267,"avatar_url":"https://avatars.githubusercontent.com/u/437267?v=3","gravatar_id":"","url":"https://api.github.com/users/elasticsales","html_url":"https://github.com/elasticsales","followers_url":"https://api.github.com/users/elasticsales/followers","following_url":"https://api.github.com/users/elasticsales/following{/other_user}","gists_url":"https://api.github.com/users/elasticsales/gists{/gist_id}","starred_url":"https://api.github.com/users/elasticsales/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elasticsales/subscriptions","organizations_url":"https://api.github.com/users/elasticsales/orgs","repos_url":"https://api.github.com/users/elasticsales/repos","events_url":"https://api.github.com/users/elasticsales/events{/privacy}","received_events_url":"https://api.github.com/users/elasticsales/received_events","type":"Organization","site_admin":false},"repo":{"id":5533627,"name":"mongoengine","full_name":"elasticsales/mongoengine","owner":{"login":"elasticsales","id":437267,"avatar_url":"https://avatars.githubusercontent.com/u/437267?v=3","gravatar_id":"","url":"https://api.github.com/users/elasticsales","html_url":"https://github.com/elasticsales","followers_url":"https://api.github.com/users/elasticsales/followers","following_url":"https://api.github.com/users/elasticsales/following{/other_user}","gists_url":"https://api.github.com/users/elasticsales/gists{/gist_id}","starred_url":"https://api.github.com/users/elasticsales/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elasticsales/subscriptions","organizations_url":"https://api.github.com/users/elasticsales/orgs","repos_url":"https://api.github.com/users/elasticsales/repos","events_url":"https://api.github.com/users/elasticsales/events{/privacy}","received_events_url":"https://api.github.com/users/elasticsales/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elasticsales/mongoengine","description":"A Python Object-Document-Mapper for working with MongoDB","fork":true,"url":"https://api.github.com/repos/elasticsales/mongoengine","forks_url":"https://api.github.com/repos/elasticsales/mongoengine/forks","keys_url":"https://api.github.com/repos/elasticsales/mongoengine/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elasticsales/mongoengine/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elasticsales/mongoengine/teams","hooks_url":"https://api.github.com/repos/elasticsales/mongoengine/hooks","issue_events_url":"https://api.github.com/repos/elasticsales/mongoengine/issues/events{/number}","events_url":"https://api.github.com/repos/elasticsales/mongoengine/events","assignees_url":"https://api.github.com/repos/elasticsales/mongoengine/assignees{/user}","branches_url":"https://api.github.com/repos/elasticsales/mongoengine/branches{/branch}","tags_url":"https://api.github.com/repos/elasticsales/mongoengine/tags","blobs_url":"https://api.github.com/repos/elasticsales/mongoengine/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elasticsales/mongoengine/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elasticsales/mongoengine/git/refs{/sha}","trees_url":"https://api.github.com/repos/elasticsales/mongoengine/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elasticsales/mongoengine/statuses/{sha}","languages_url":"https://api.github.com/repos/elasticsales/mongoengine/languages","stargazers_url":"https://api.github.com/repos/elasticsales/mongoengine/stargazers","contributors_url":"https://api.github.com/repos/elasticsales/mongoengine/contributors","subscribers_url":"https://api.github.com/repos/elasticsales/mongoengine/subscribers","subscription_url":"https://api.github.com/repos/elasticsales/mongoengine/subscription","commits_url":"https://api.github.com/repos/elasticsales/mongoengine/commits{/sha}","git_commits_url":"https://api.github.com/repos/elasticsales/mongoengine/git/commits{/sha}","comments_url":"https://api.github.com/repos/elasticsales/mongoengine/comments{/number}","issue_comment_url":"https://api.github.com/repos/elasticsales/mongoengine/issues/comments{/number}","contents_url":"https://api.github.com/repos/elasticsales/mongoengine/contents/{+path}","compare_url":"https://api.github.com/repos/elasticsales/mongoengine/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elasticsales/mongoengine/merges","archive_url":"https://api.github.com/repos/elasticsales/mongoengine/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elasticsales/mongoengine/downloads","issues_url":"https://api.github.com/repos/elasticsales/mongoengine/issues{/number}","pulls_url":"https://api.github.com/repos/elasticsales/mongoengine/pulls{/number}","milestones_url":"https://api.github.com/repos/elasticsales/mongoengine/milestones{/number}","notifications_url":"https://api.github.com/repos/elasticsales/mongoengine/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elasticsales/mongoengine/labels{/name}","releases_url":"https://api.github.com/repos/elasticsales/mongoengine/releases{/id}","created_at":"2012-08-23T23:02:20Z","updated_at":"2015-06-25T03:29:38Z","pushed_at":"2015-06-25T06:53:21Z","git_url":"git://github.com/elasticsales/mongoengine.git","ssh_url":"git@github.com:elasticsales/mongoengine.git","clone_url":"https://github.com/elasticsales/mongoengine.git","svn_url":"https://github.com/elasticsales/mongoengine","homepage":"http://mongoengine.org","size":8456,"stargazers_count":22,"watchers_count":22,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":8,"mirror_url":null,"open_issues_count":1,"forks":8,"open_issues":1,"watchers":22,"default_branch":"master"}},"base":{"label":"MongoEngine:master","ref":"master","sha":"c6151e34e0a3f87c5d8ceb409c968a1f8cdee4a6","user":{"login":"MongoEngine","id":1502485,"avatar_url":"https://avatars.githubusercontent.com/u/1502485?v=3","gravatar_id":"","url":"https://api.github.com/users/MongoEngine","html_url":"https://github.com/MongoEngine","followers_url":"https://api.github.com/users/MongoEngine/followers","following_url":"https://api.github.com/users/MongoEngine/following{/other_user}","gists_url":"https://api.github.com/users/MongoEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/MongoEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MongoEngine/subscriptions","organizations_url":"https://api.github.com/users/MongoEngine/orgs","repos_url":"https://api.github.com/users/MongoEngine/repos","events_url":"https://api.github.com/users/MongoEngine/events{/privacy}","received_events_url":"https://api.github.com/users/MongoEngine/received_events","type":"Organization","site_admin":false},"repo":{"id":3626679,"name":"mongoengine","full_name":"MongoEngine/mongoengine","owner":{"login":"MongoEngine","id":1502485,"avatar_url":"https://avatars.githubusercontent.com/u/1502485?v=3","gravatar_id":"","url":"https://api.github.com/users/MongoEngine","html_url":"https://github.com/MongoEngine","followers_url":"https://api.github.com/users/MongoEngine/followers","following_url":"https://api.github.com/users/MongoEngine/following{/other_user}","gists_url":"https://api.github.com/users/MongoEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/MongoEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MongoEngine/subscriptions","organizations_url":"https://api.github.com/users/MongoEngine/orgs","repos_url":"https://api.github.com/users/MongoEngine/repos","events_url":"https://api.github.com/users/MongoEngine/events{/privacy}","received_events_url":"https://api.github.com/users/MongoEngine/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/MongoEngine/mongoengine","description":"A Python Object-Document-Mapper for working with MongoDB","fork":false,"url":"https://api.github.com/repos/MongoEngine/mongoengine","forks_url":"https://api.github.com/repos/MongoEngine/mongoengine/forks","keys_url":"https://api.github.com/repos/MongoEngine/mongoengine/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MongoEngine/mongoengine/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MongoEngine/mongoengine/teams","hooks_url":"https://api.github.com/repos/MongoEngine/mongoengine/hooks","issue_events_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/events{/number}","events_url":"https://api.github.com/repos/MongoEngine/mongoengine/events","assignees_url":"https://api.github.com/repos/MongoEngine/mongoengine/assignees{/user}","branches_url":"https://api.github.com/repos/MongoEngine/mongoengine/branches{/branch}","tags_url":"https://api.github.com/repos/MongoEngine/mongoengine/tags","blobs_url":"https://api.github.com/repos/MongoEngine/mongoengine/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MongoEngine/mongoengine/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MongoEngine/mongoengine/git/refs{/sha}","trees_url":"https://api.github.com/repos/MongoEngine/mongoengine/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MongoEngine/mongoengine/statuses/{sha}","languages_url":"https://api.github.com/repos/MongoEngine/mongoengine/languages","stargazers_url":"https://api.github.com/repos/MongoEngine/mongoengine/stargazers","contributors_url":"https://api.github.com/repos/MongoEngine/mongoengine/contributors","subscribers_url":"https://api.github.com/repos/MongoEngine/mongoengine/subscribers","subscription_url":"https://api.github.com/repos/MongoEngine/mongoengine/subscription","commits_url":"https://api.github.com/repos/MongoEngine/mongoengine/commits{/sha}","git_commits_url":"https://api.github.com/repos/MongoEngine/mongoengine/git/commits{/sha}","comments_url":"https://api.github.com/repos/MongoEngine/mongoengine/comments{/number}","issue_comment_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/comments{/number}","contents_url":"https://api.github.com/repos/MongoEngine/mongoengine/contents/{+path}","compare_url":"https://api.github.com/repos/MongoEngine/mongoengine/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MongoEngine/mongoengine/merges","archive_url":"https://api.github.com/repos/MongoEngine/mongoengine/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MongoEngine/mongoengine/downloads","issues_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues{/number}","pulls_url":"https://api.github.com/repos/MongoEngine/mongoengine/pulls{/number}","milestones_url":"https://api.github.com/repos/MongoEngine/mongoengine/milestones{/number}","notifications_url":"https://api.github.com/repos/MongoEngine/mongoengine/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MongoEngine/mongoengine/labels{/name}","releases_url":"https://api.github.com/repos/MongoEngine/mongoengine/releases{/id}","created_at":"2012-03-05T12:10:20Z","updated_at":"2015-06-24T02:46:02Z","pushed_at":"2015-06-25T07:32:00Z","git_url":"git://github.com/MongoEngine/mongoengine.git","ssh_url":"git@github.com:MongoEngine/mongoengine.git","clone_url":"https://github.com/MongoEngine/mongoengine.git","svn_url":"https://github.com/MongoEngine/mongoengine","homepage":"http://mongoengine.org","size":13683,"stargazers_count":1001,"watchers_count":1001,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":615,"mirror_url":null,"open_issues_count":194,"forks":615,"open_issues":194,"watchers":1001,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042"},"html":{"href":"https://github.com/MongoEngine/mongoengine/pull/1042"},"issue":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/issues/1042"},"comments":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/issues/1042/comments"},"review_comments":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042/comments"},"review_comment":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/pulls/1042/commits"},"statuses":{"href":"https://api.github.com/repos/MongoEngine/mongoengine/statuses/3f803372f8d3bdf8623a98b2945f8b76762a93d7"}},"merged":false,"mergeable":true,"mergeable_state":"clean","merged_by":null,"comments":0,"review_comments":0,"commits":1,"additions":49,"deletions":1,"changed_files":3}}