简体   繁体   中英

Spring Data- MongoDB geoNear not getting distributed in replicaset

We are using a replica-set of MongoDB with 1 Primary, 1 Secondary and 1 Arbiter.

At our java application, we are using Spring-data-mongodb to interact with MongoDB. A key use case in our application involves using an aggregation pipeline with geoNear query with distance sorting which is something like this (not including the query part of this operation - but it involves checking non-geographic fields like status checks of the locations

[ { "$geoNear" : { "query" : {  ..... } , 
"maxDistance" : 4.7035678286621944E-5 , "distanceMultiplier" : 6378.137 , 
"near" : [ 77.09979953386978 , 28.442629624873796] , "spherical" : true , "distanceField" : "distance"}} ,
 { "$project" : { "distance" : 1}}]

We have set the replica preference of read operations. However, regardless of whether we use NEAREST or SECONDARY_PREFERRED as our read preference, the geonear queries are always directed to the primary and is not balanced across the replica-set.

Are we missing something here? As per mongodb operation geoNear query is included in the operations that respect the read preference specified.

It turned out that spring-data-mongodb does not pass the read preference specified on mongoTemplate to the underlying com.mongodb.Mongo connection pool - for aggregations. It would pass the read preference for simple find operations.

We had to modify our spring configuration - not using spring data's mongo:mongo which did not provide the facility of specifying read preference - but writing our own Factory bean for creating the same where we could set the read preference on com.mongodb.Mongo. With that we were able to direct our geonear aggregation queries also to whatever readpreference we specified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM