@@ -4469,6 +4469,51 @@ describe('Query', function() {
4469
4469
} ) ;
4470
4470
} ) ;
4471
4471
4472
+ it ( 'propagates readPreference to populate options if read() is called after populate() (gh-15553)' , async function ( ) {
4473
+ const schema = new Schema ( { name : String , age : Number , friends : [ { type : 'ObjectId' , ref : 'Person' } ] } ) ;
4474
+ const Person = db . model ( 'Person' , schema ) ;
4475
+
4476
+ let query = Person . find ( { } ) . populate ( 'friends' ) ;
4477
+ query . read ( 'secondaryPreferred' ) ;
4478
+ await query . exec ( ) ;
4479
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readPreference . mode , 'secondaryPreferred' ) ;
4480
+
4481
+ query = Person . find ( { } ) . read ( 'secondary' ) . populate ( 'friends' ) ;
4482
+ query . read ( 'secondaryPreferred' ) ;
4483
+ await query . exec ( ) ;
4484
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readPreference . mode , 'secondaryPreferred' ) ;
4485
+
4486
+ query = Person . find ( { } ) . read ( 'secondaryPreferred' ) . populate ( 'friends' ) ;
4487
+ await query . exec ( ) ;
4488
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readPreference . mode , 'secondaryPreferred' ) ;
4489
+
4490
+ query = Person . find ( { } ) . read ( 'primaryPreferred' ) . populate ( { path : 'friends' , options : { readPreference : 'secondaryPreferred' } } ) ;
4491
+ await query . exec ( ) ;
4492
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readPreference , 'secondaryPreferred' ) ;
4493
+ } ) ;
4494
+
4495
+ it ( 'propagates readConcern to populate options if readConcern() is called after populate() (gh-15553)' , async function ( ) {
4496
+ const schema = new Schema ( { name : String , age : Number , friends : [ { type : 'ObjectId' , ref : 'Person' } ] } ) ;
4497
+ const Person = db . model ( 'Person' , schema ) ;
4498
+
4499
+ let query = Person . find ( { } ) . populate ( 'friends' ) ;
4500
+ query . readConcern ( 'majority' ) ;
4501
+ await query . exec ( ) ;
4502
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readConcern . level , 'majority' ) ;
4503
+
4504
+ query = Person . find ( { } ) . readConcern ( 'local' ) . populate ( 'friends' ) ;
4505
+ query . readConcern ( 'majority' ) ;
4506
+ await query . exec ( ) ;
4507
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readConcern . level , 'majority' ) ;
4508
+
4509
+ query = Person . find ( { } ) . readConcern ( 'majority' ) . populate ( 'friends' ) ;
4510
+ await query . exec ( ) ;
4511
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readConcern . level , 'majority' ) ;
4512
+
4513
+ query = Person . find ( { } ) . readConcern ( 'majority' ) . populate ( { path : 'friends' , options : { readConcern : 'local' } } ) ;
4514
+ await query . exec ( ) ;
4515
+ assert . strictEqual ( query . _mongooseOptions . populate . friends . options . readConcern , 'local' ) ;
4516
+ } ) ;
4472
4517
4473
4518
describe ( 'Query with requireFilter' , function ( ) {
4474
4519
let Person ;
0 commit comments