Skip to content

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

@MikeKoval

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions