'Spring boot MongoDB allowSecondaryReads() is not working

Query query = new Query();

PageRequest pageRequest = PageRequest.of(0,20);

query.with(pageRequest);

//query.allowSecondaryReads() -> doesn't really make the query to secondary node

mongoTemplate.find(query.allowSecondaryReads(), User.class);

//also @Meta doesn't call the secondary node

public interface FooBarRepository extends MongoRepository<FooBar, String> {

  @Query("{}")
  @Meta(flags = Meta.CursorOption.SECONDARY_READS)
  FooBar someQuery();
}

//collection.find(query.getQueryObject()) calls the secondary but doesn't respect the pagination

MongoCollection<Document> fooCollection = mongoTemplate.getCollection("Foo").withReadPreference(ReadPreference.secondary());
ArrayList<Document> docArrayList = fooCollection.find(query.getQueryObject()).into(new ArrayList<>());

-- Please suggest the code to successfully reading from secondary.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source