Skip to content

Setting read pref after populate in query chain causing main query run on secondary while populated one on primary #15553

Closed

Description

@MikeKoval

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Mongoose ^7.0.3
Node.js ^22

Issue

Hi!

There is one query that looks like this:

const readPreference = 'secondaryPreferred';
 const getItems= Vendor.find(filter, {
      name: 1
    })
    .read(readPreference)
    .populate('populateItems');
...


await getItems.exec();

There is another query that looks like this

const readPreference = 'secondaryPreferred';
 const getItems= Vendor.find(filter, {
      name: 1
    })
    .populate('populateItems');
...


await getItems.read(readPreference).exec();

Interesting but in the first case both queries are run on secondary, while in second case in mongoose logs with enabled debug i see that only Vendor.find is run on secondary, while .populate('populateItems') is run on primary.

Is it expected documented behavior that i missed or a bug?

Activity

  1. added this to the 8.17.1 milestone on Aug 4, 2025
  2. added
    needs repro script
    confirmed-bug
    and removed
    needs repro script
    on Aug 4, 2025
  3. added 5 commits that reference this issue on Aug 4, 2025
    ee51c83
    5f538e0
    70ac7f1
    3ca038e
    c05baa5
  4. added a commit that references this issue on Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions