Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4435

Cloning Criteria does not preserve read preference

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0, 6.1.1, origin-2.3.1
    • Affects Version/s: 5.2.0, 5.2.1
    • Component/s: None
    • None

      In the example below, we can see that `col1.first` reads from port 20001 (secondary) and that's considered correct. After we clone col1, pretty print shows same options (notice `:mode` becoming `"mode"`), but `col1.clone.first` reads from port 20000, which is assigned to the primary. The same thing happens if we do `col1.where(anything)` since `.where` internally clones the selection.

      ```ruby
      [152] app(PROD)> col1
      => #<Mongoid::Criteria
      selector:

      {"app_id"=>6}
      options: {:read=>{:mode=>:secondary}}
      class: External::User
      embedded: false>
      [153] app(PROD)> col1.first;
      MONGODB | 127.0.0.1:20001 | production_users_2.find | STARTED | {"find"=>"users", "filter"=>{"app_id"=>6}

      , "limit"=>1, "singleBatch"=>true}
      MONGODB | 127.0.0.1:20001 | production_users_2.find | SUCCEEDED | 0.0020012600000000004s
      [155] app(PROD)> col1.clone
      => #<Mongoid::Criteria
      selector:

      {"app_id"=>6}
      options: {:read=>{"mode"=>:secondary}}
      class: External::User
      embedded: false>
      [156] app(PROD)> col1.clone.first
      MONGODB | 127.0.0.1:20000 | production_users_2.find | STARTED | {"find"=>"users", "filter"=>{"app_id"=>6}

      , "limit"=>1, "singleBatch"=>true}
      MONGODB | 127.0.0.1:20000 | production_users_2.find | SUCCEEDED | 0.002215638s
      ```

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            mario@intercom.io Mario Kostelac
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: