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 https://github.com/parse-community/parse-server/pull/6540 * fixed duplicate index name in test
This commit is contained in:
@@ -1430,7 +1430,7 @@ describe('Parse.Query Aggregate testing', () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
it_only_db('mongo')('geoNear with location query', async () => {
|
it_only_db('mongo')('aggregate geoNear with location query', async () => {
|
||||||
// Create geo index which is required for `geoNear` query
|
// Create geo index which is required for `geoNear` query
|
||||||
const database = Config.get(Parse.applicationId).database;
|
const database = Config.get(Parse.applicationId).database;
|
||||||
const schema = await new Parse.Schema('GeoObject').save();
|
const schema = await new Parse.Schema('GeoObject').save();
|
||||||
@@ -1438,7 +1438,7 @@ describe('Parse.Query Aggregate testing', () => {
|
|||||||
'GeoObject',
|
'GeoObject',
|
||||||
schema,
|
schema,
|
||||||
['location'],
|
['location'],
|
||||||
'geoIndex',
|
undefined,
|
||||||
false,
|
false,
|
||||||
{ indexType: '2dsphere' },
|
{ indexType: '2dsphere' },
|
||||||
);
|
);
|
||||||
@@ -1474,4 +1474,77 @@ describe('Parse.Query Aggregate testing', () => {
|
|||||||
expect(results[0].value).toEqual(2);
|
expect(results[0].value).toEqual(2);
|
||||||
expect(results[1].value).toEqual(3);
|
expect(results[1].value).toEqual(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it_only_db('mongo')('aggregate geoNear with near GeoJSON point', async () => {
|
||||||
|
// Create geo index which is required for `geoNear` query
|
||||||
|
const database = Config.get(Parse.applicationId).database;
|
||||||
|
const schema = await new Parse.Schema('GeoObject').save();
|
||||||
|
await database.adapter.ensureIndex(
|
||||||
|
'GeoObject',
|
||||||
|
schema,
|
||||||
|
['location'],
|
||||||
|
undefined,
|
||||||
|
false,
|
||||||
|
'2dsphere'
|
||||||
|
);
|
||||||
|
// Create objects
|
||||||
|
const GeoObject = Parse.Object.extend('GeoObject');
|
||||||
|
const obj1 = new GeoObject({ value: 1, location: new Parse.GeoPoint(1, 1), date: new Date(1) });
|
||||||
|
const obj2 = new GeoObject({ value: 2, location: new Parse.GeoPoint(2, 1), date: new Date(2) });
|
||||||
|
const obj3 = new GeoObject({ value: 3, location: new Parse.GeoPoint(3, 1), date: new Date(3) });
|
||||||
|
await Parse.Object.saveAll([obj1, obj2, obj3]);
|
||||||
|
// Create query
|
||||||
|
const pipeline = [
|
||||||
|
{
|
||||||
|
geoNear: {
|
||||||
|
near: {
|
||||||
|
type: 'Point',
|
||||||
|
coordinates: [1, 1]
|
||||||
|
},
|
||||||
|
key: 'location',
|
||||||
|
spherical: true,
|
||||||
|
distanceField: 'dist'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const query = new Parse.Query(GeoObject);
|
||||||
|
const results = await query.aggregate(pipeline);
|
||||||
|
// Check results
|
||||||
|
expect(results.length).toEqual(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it_only_db('mongo')('aggregate geoNear with near legacy coordinate pair', async () => {
|
||||||
|
// Create geo index which is required for `geoNear` query
|
||||||
|
const database = Config.get(Parse.applicationId).database;
|
||||||
|
const schema = await new Parse.Schema('GeoObject').save();
|
||||||
|
await database.adapter.ensureIndex(
|
||||||
|
'GeoObject',
|
||||||
|
schema,
|
||||||
|
['location'],
|
||||||
|
undefined,
|
||||||
|
false,
|
||||||
|
'2dsphere'
|
||||||
|
);
|
||||||
|
// Create objects
|
||||||
|
const GeoObject = Parse.Object.extend('GeoObject');
|
||||||
|
const obj1 = new GeoObject({ value: 1, location: new Parse.GeoPoint(1, 1), date: new Date(1) });
|
||||||
|
const obj2 = new GeoObject({ value: 2, location: new Parse.GeoPoint(2, 1), date: new Date(2) });
|
||||||
|
const obj3 = new GeoObject({ value: 3, location: new Parse.GeoPoint(3, 1), date: new Date(3) });
|
||||||
|
await Parse.Object.saveAll([obj1, obj2, obj3]);
|
||||||
|
// Create query
|
||||||
|
const pipeline = [
|
||||||
|
{
|
||||||
|
geoNear: {
|
||||||
|
near: [1, 1],
|
||||||
|
key: 'location',
|
||||||
|
spherical: true,
|
||||||
|
distanceField: 'dist'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const query = new Parse.Query(GeoObject);
|
||||||
|
const results = await query.aggregate(pipeline);
|
||||||
|
// Check results
|
||||||
|
expect(results.length).toEqual(3);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -860,7 +860,7 @@ export class MongoStorageAdapter implements StorageAdapter {
|
|||||||
stage.$project
|
stage.$project
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (stage.$geoNear) {
|
if (stage.$geoNear && stage.$geoNear.query) {
|
||||||
stage.$geoNear.query = this._parseAggregateArgs(
|
stage.$geoNear.query = this._parseAggregateArgs(
|
||||||
schema,
|
schema,
|
||||||
stage.$geoNear.query
|
stage.$geoNear.query
|
||||||
|
|||||||
Reference in New Issue
Block a user