Skip to content

Conversation

vkarpov15
Copy link
Collaborator

Fix #15553

Summary

#15553 points out that TestModel.findOne().read('secondaryPreferred').populate('friends') applies read preference to populate queries, but TestModel.findOne().populate('friends').read('secondaryPreferred') currently does not. With this PR, we'll apply top-level read preference to all populate queries when executing the query.

Examples

@vkarpov15 vkarpov15 added this to the 8.17.1 milestone Aug 4, 2025
Copilot

This comment was marked as outdated.

vkarpov15 and others added 2 commits August 4, 2025 12:01
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@vkarpov15 vkarpov15 requested a review from Copilot August 6, 2025 13:52
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where read preferences and read concerns were not properly propagated to populate queries when read() or readConcern() methods were called after populate(). The fix ensures that read preferences and read concerns are consistently applied to populate operations regardless of the method call order.

  • Moved read preference/concern propagation logic from populate() method to _optionsForExec() method
  • Added comprehensive test coverage for both read preference and read concern propagation scenarios
  • Fixed existing test to include proper execution for assertion validation

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
lib/query.js Relocated read preference/concern propagation logic from populate method to execution options preparation
test/query.test.js Added comprehensive test cases for read preference and read concern propagation scenarios
test/model.populate.test.js Fixed existing test by adding missing query execution before assertion
Comments suppressed due to low confidence (3)

test/query.test.js:4479

  • This test accesses internal implementation details (_mongooseOptions) rather than testing the actual behavior. Consider testing the actual MongoDB queries or using a mock to verify the read preference is applied correctly.
    assert.strictEqual(query._mongooseOptions.populate.friends.options.readPreference.mode, 'secondaryPreferred');

test/query.test.js:4492

  • This assertion checks for a string value while other assertions in the same test check for .mode property. This inconsistency suggests the test may not be validating the correct behavior when explicit readPreference options are provided.
    assert.strictEqual(query._mongooseOptions.populate.friends.options.readPreference, 'secondaryPreferred');

test/query.test.js:4515

  • This assertion checks for a string value while other assertions in the same test check for .level property. This inconsistency suggests the test may not be validating the correct behavior when explicit readConcern options are provided.
    assert.strictEqual(query._mongooseOptions.populate.friends.options.readConcern, 'local');

@vkarpov15 vkarpov15 merged commit d333d8e into master Aug 6, 2025
72 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15553 branch August 6, 2025 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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