Skip to content

Commit 5fd73a7

Browse files
authored
fix potential issue with setting geoNear.query to undefined (#6696)
* add test cases for geoNear aggregation Test cases do not have the `query` parameter set in $geoNear aggregation stage. this is to test for a reported potential issue when the parameter is not set. * fixed potential issue when setting the geoNear.query parameter to undefined see dicussion in #6540 * fixed duplicate index name in test
1 parent 2e8e193 commit 5fd73a7

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

spec/ParseQuery.Aggregate.spec.js

+75-2
Original file line numberDiff line numberDiff line change
@@ -1430,15 +1430,15 @@ describe('Parse.Query Aggregate testing', () => {
14301430
}
14311431
);
14321432

1433-
it_only_db('mongo')('geoNear with location query', async () => {
1433+
it_only_db('mongo')('aggregate geoNear with location query', async () => {
14341434
// Create geo index which is required for `geoNear` query
14351435
const database = Config.get(Parse.applicationId).database;
14361436
const schema = await new Parse.Schema('GeoObject').save();
14371437
await database.adapter.ensureIndex(
14381438
'GeoObject',
14391439
schema,
14401440
['location'],
1441-
'geoIndex',
1441+
undefined,
14421442
false,
14431443
{ indexType: '2dsphere' },
14441444
);
@@ -1474,4 +1474,77 @@ describe('Parse.Query Aggregate testing', () => {
14741474
expect(results[0].value).toEqual(2);
14751475
expect(results[1].value).toEqual(3);
14761476
});
1477+
1478+
it_only_db('mongo')('aggregate geoNear with near GeoJSON point', async () => {
1479+
// Create geo index which is required for `geoNear` query
1480+
const database = Config.get(Parse.applicationId).database;
1481+
const schema = await new Parse.Schema('GeoObject').save();
1482+
await database.adapter.ensureIndex(
1483+
'GeoObject',
1484+
schema,
1485+
['location'],
1486+
undefined,
1487+
false,
1488+
'2dsphere'
1489+
);
1490+
// Create objects
1491+
const GeoObject = Parse.Object.extend('GeoObject');
1492+
const obj1 = new GeoObject({ value: 1, location: new Parse.GeoPoint(1, 1), date: new Date(1) });
1493+
const obj2 = new GeoObject({ value: 2, location: new Parse.GeoPoint(2, 1), date: new Date(2) });
1494+
const obj3 = new GeoObject({ value: 3, location: new Parse.GeoPoint(3, 1), date: new Date(3) });
1495+
await Parse.Object.saveAll([obj1, obj2, obj3]);
1496+
// Create query
1497+
const pipeline = [
1498+
{
1499+
geoNear: {
1500+
near: {
1501+
type: 'Point',
1502+
coordinates: [1, 1]
1503+
},
1504+
key: 'location',
1505+
spherical: true,
1506+
distanceField: 'dist'
1507+
}
1508+
}
1509+
];
1510+
const query = new Parse.Query(GeoObject);
1511+
const results = await query.aggregate(pipeline);
1512+
// Check results
1513+
expect(results.length).toEqual(3);
1514+
});
1515+
1516+
it_only_db('mongo')('aggregate geoNear with near legacy coordinate pair', async () => {
1517+
// Create geo index which is required for `geoNear` query
1518+
const database = Config.get(Parse.applicationId).database;
1519+
const schema = await new Parse.Schema('GeoObject').save();
1520+
await database.adapter.ensureIndex(
1521+
'GeoObject',
1522+
schema,
1523+
['location'],
1524+
undefined,
1525+
false,
1526+
'2dsphere'
1527+
);
1528+
// Create objects
1529+
const GeoObject = Parse.Object.extend('GeoObject');
1530+
const obj1 = new GeoObject({ value: 1, location: new Parse.GeoPoint(1, 1), date: new Date(1) });
1531+
const obj2 = new GeoObject({ value: 2, location: new Parse.GeoPoint(2, 1), date: new Date(2) });
1532+
const obj3 = new GeoObject({ value: 3, location: new Parse.GeoPoint(3, 1), date: new Date(3) });
1533+
await Parse.Object.saveAll([obj1, obj2, obj3]);
1534+
// Create query
1535+
const pipeline = [
1536+
{
1537+
geoNear: {
1538+
near: [1, 1],
1539+
key: 'location',
1540+
spherical: true,
1541+
distanceField: 'dist'
1542+
}
1543+
}
1544+
];
1545+
const query = new Parse.Query(GeoObject);
1546+
const results = await query.aggregate(pipeline);
1547+
// Check results
1548+
expect(results.length).toEqual(3);
1549+
});
14771550
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ export class MongoStorageAdapter implements StorageAdapter {
860860
stage.$project
861861
);
862862
}
863-
if (stage.$geoNear) {
863+
if (stage.$geoNear && stage.$geoNear.query) {
864864
stage.$geoNear.query = this._parseAggregateArgs(
865865
schema,
866866
stage.$geoNear.query

0 commit comments

Comments
 (0)