简体   繁体   English

Spring Data-MongoDB geoNear没有在副本集中分发

[英]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. 我们正在使用具有1个主节点,1个辅助节点和1个仲裁器的MongoDB副本集。

At our java application, we are using Spring-data-mongodb to interact with MongoDB. 在我们的Java应用程序中,我们使用Spring-data-mongodb与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查询一起使用(类似于此操作(不包括此操作的查询部分),但是它涉及检查非地理字段,例如位置的状态检查)

[ { "$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. 但是,无论我们使用NEAREST还是SECONDARY_PREFERRED作为读取首选项,geonear查询始终会定向到主查询,并且不会在副本集之间保持平衡。

Are we missing something here? 我们在这里缺少什么吗? As per mongodb operation geoNear query is included in the operations that respect the read preference specified. 根据mongodb操作,尊重指定的读取首选项的操作中包含geoNear查询。

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. 事实证明,spring-data-mongodb不会将mongoTemplate上指定的读取首选项传递给基础com.mongodb.Mongo连接池-用于聚合。 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. 我们必须修改我们的spring配置-不使用spring数据的mongo:mongo,该mongo:mongo不提供指定读取首选项的功能-而是编写我们自己的Factory bean来创建可以在com.mongodb.Mongo上设置读取首选项的工厂bean。 With that we were able to direct our geonear aggregation queries also to whatever readpreference we specified. 这样,我们就可以将geonear聚合查询也定向到我们指定的任何读取首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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