MongoEngine specify read preference on query












2














I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows :



from pymongo.read_preferences import ReadPreference    
<collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED)


However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried?










share|improve this question






















  • Could be due to this bug if you're chaining another method such as skip, limit, etc.
    – Jérôme
    May 12 '16 at 16:05










  • I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
    – Yahya
    May 13 '16 at 7:43












  • The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
    – Jérôme
    May 13 '16 at 8:44
















2














I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows :



from pymongo.read_preferences import ReadPreference    
<collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED)


However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried?










share|improve this question






















  • Could be due to this bug if you're chaining another method such as skip, limit, etc.
    – Jérôme
    May 12 '16 at 16:05










  • I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
    – Yahya
    May 13 '16 at 7:43












  • The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
    – Jérôme
    May 13 '16 at 8:44














2












2








2







I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows :



from pymongo.read_preferences import ReadPreference    
<collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED)


However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried?










share|improve this question













I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows :



from pymongo.read_preferences import ReadPreference    
<collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED)


However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried?







mongodb pymongo mongoengine replicaset






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 12 '16 at 9:48









Yahya

1991620




1991620












  • Could be due to this bug if you're chaining another method such as skip, limit, etc.
    – Jérôme
    May 12 '16 at 16:05










  • I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
    – Yahya
    May 13 '16 at 7:43












  • The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
    – Jérôme
    May 13 '16 at 8:44


















  • Could be due to this bug if you're chaining another method such as skip, limit, etc.
    – Jérôme
    May 12 '16 at 16:05










  • I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
    – Yahya
    May 13 '16 at 7:43












  • The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
    – Jérôme
    May 13 '16 at 8:44
















Could be due to this bug if you're chaining another method such as skip, limit, etc.
– Jérôme
May 12 '16 at 16:05




Could be due to this bug if you're chaining another method such as skip, limit, etc.
– Jérôme
May 12 '16 at 16:05












I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
– Yahya
May 13 '16 at 7:43






I am chaining it with the order_by method but my Pymongo version is 2.7.2 and the bug is in any version >= 3.0. I guess it is broken in 2.7.2 as well.
– Yahya
May 13 '16 at 7:43














The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
– Jérôme
May 13 '16 at 8:44




The message reads "As of now, the fix only works for pymongo ver < 3.0.", so I thought both 2.7 and 3+ where affected (although differently). You may want to try without order_by, to double-check.
– Jérôme
May 13 '16 at 8:44












1 Answer
1






active

oldest

votes


















1














Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:



connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")



The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37183256%2fmongoengine-specify-read-preference-on-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:



    connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")



    The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.






    share|improve this answer


























      1














      Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:



      connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")



      The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.






      share|improve this answer
























        1












        1








        1






        Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:



        connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")



        The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.






        share|improve this answer












        Figured out what the issue was. In the MongoEngine "connect" method, the replicaSet parameter needed to be specified as follows:



        connect(db = "my_db", replicaSet = "my_replica_set_name", host = "hostname", port = "port_number")



        The syntax of the read preference is correct as specified above. Passing in the replicaSet parameter made it work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 1 '16 at 6:34









        Yahya

        1991620




        1991620






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37183256%2fmongoengine-specify-read-preference-on-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'