@@ -224,7 +224,7 @@ const valueAsDate = value => {
224
224
return false ;
225
225
} ;
226
226
227
- function transformQueryKeyValue ( className , key , value , schema ) {
227
+ function transformQueryKeyValue ( className , key , value , schema , count = false ) {
228
228
switch ( key ) {
229
229
case 'createdAt' :
230
230
if ( valueAsDate ( value ) ) {
@@ -293,7 +293,7 @@ function transformQueryKeyValue(className, key, value, schema) {
293
293
return {
294
294
key : key ,
295
295
value : value . map ( subQuery =>
296
- transformWhere ( className , subQuery , schema )
296
+ transformWhere ( className , subQuery , schema , count )
297
297
) ,
298
298
} ;
299
299
case 'lastUsed' :
@@ -330,7 +330,7 @@ function transformQueryKeyValue(className, key, value, schema) {
330
330
}
331
331
332
332
// Handle query constraints
333
- const transformedConstraint = transformConstraint ( value , field ) ;
333
+ const transformedConstraint = transformConstraint ( value , field , count ) ;
334
334
if ( transformedConstraint !== CannotTransform ) {
335
335
if ( transformedConstraint . $text ) {
336
336
return { key : '$text' , value : transformedConstraint . $text } ;
@@ -359,14 +359,15 @@ function transformQueryKeyValue(className, key, value, schema) {
359
359
// Main exposed method to help run queries.
360
360
// restWhere is the "where" clause in REST API form.
361
361
// Returns the mongo form of the query.
362
- function transformWhere ( className , restWhere , schema ) {
362
+ function transformWhere ( className , restWhere , schema , count = false ) {
363
363
const mongoWhere = { } ;
364
364
for ( const restKey in restWhere ) {
365
365
const out = transformQueryKeyValue (
366
366
className ,
367
367
restKey ,
368
368
restWhere [ restKey ] ,
369
- schema
369
+ schema ,
370
+ count
370
371
) ;
371
372
mongoWhere [ out . key ] = out . value ;
372
373
}
@@ -816,7 +817,7 @@ function relativeTimeToDate(text, now = new Date()) {
816
817
// If it is not a valid constraint but it could be a valid something
817
818
// else, return CannotTransform.
818
819
// inArray is whether this is an array field.
819
- function transformConstraint ( constraint , field ) {
820
+ function transformConstraint ( constraint , field , count = false ) {
820
821
const inArray = field && field . type && field . type === 'Array' ;
821
822
if ( typeof constraint !== 'object' || ! constraint ) {
822
823
return CannotTransform ;
@@ -1002,15 +1003,27 @@ function transformConstraint(constraint, field) {
1002
1003
}
1003
1004
break ;
1004
1005
}
1005
- case '$nearSphere' :
1006
- var point = constraint [ key ] ;
1007
- answer [ key ] = [ point . longitude , point . latitude ] ;
1006
+ case '$nearSphere' : {
1007
+ const point = constraint [ key ] ;
1008
+ if ( count ) {
1009
+ answer . $geoWithin = {
1010
+ $centerSphere : [
1011
+ [ point . longitude , point . latitude ] ,
1012
+ constraint . $maxDistance ,
1013
+ ] ,
1014
+ } ;
1015
+ } else {
1016
+ answer [ key ] = [ point . longitude , point . latitude ] ;
1017
+ }
1008
1018
break ;
1009
-
1010
- case '$maxDistance' :
1019
+ }
1020
+ case '$maxDistance' : {
1021
+ if ( count ) {
1022
+ break ;
1023
+ }
1011
1024
answer [ key ] = constraint [ key ] ;
1012
1025
break ;
1013
-
1026
+ }
1014
1027
// The SDKs don't seem to use these but they are documented in the
1015
1028
// REST API docs.
1016
1029
case '$maxDistanceInRadians' :
0 commit comments