无法使用@Query

时间:2017-10-02 16:24:01

标签: spring mongodb

我在mongo存储库中有以下查询:

@Query("{ 'lastKnownLocation' : { $near : ?0}, 'username' : { $exists: true },
'lastActiveTime' : { $gte: ?1 }, 'id' : { $ne: ?2 }, 
$or: [ { 'hideAccountInPeopleNearby': false }, 
{ 'hideAccountInPeopleNearby': { $exists: false} } ] }")
List<Account> findTop6ByLastKnownLocationNearAndUsernameExistsAndLastActiveTimeBeforeAndId
NotAndHideAccountInPeopleNearbyFalse(Point lastKnownLocation, LocalDate lastActiveTime, 
String id);

它返回以下错误:

com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'error 
processing query: ns=CloudFoundry_hj345cdo_v9ifdl8t.accountTree: $and
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT     $or
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT         hideAccountInPeopleNearby == false
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT         $not
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT             hideAccountInPeopleNearby exists
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT     lastActiveTime $gte new Date(1506297600000)
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT     username exists
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT     $not
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT         _id == ObjectId('59b8f977f75ee600121406b6')
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT     GEONEAR  field=lastKnownLocation maxdist=1.79769e+308 isNearSphere=0
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT Sort: {}
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT Proj: {}
2017-10-02T16:25:44.91+0100 [APP/PROC/WEB/0] OUT  planner returned error: unable to find index for $geoNear query' on server ds059125.mlab.com:59125

这里出了什么问题?如果我删除方法上方的@Query(并添加boolean exists参数),则会正确返回值。但是,我需要查询能够返回hideAccountInPeopleNearby为false或null的值。

1 个答案:

答案 0 :(得分:0)

原来解决方案只是将$near更改为$nearSphere