Fix aggregate geoNear with date query (#6540)

* added failing test case

* add date conversion for geoNear query

- geoNear stages were not parsed for date fields, but mongodb nodejs adapter requires date object

* reverted unnecessary code auto-formatting

* limited parsing to query property of geoNear stage

- the geoNear object contains parameter keys which could be identical to field names in the collection, which should not be parsed and changed, therefore restricting parsing only to query parameter key

* reverted unnecessary code auto-formatting

* added index type parameter to ensureIndex

- required to create geo index for geoNear test

* added geo index creation to test case

* fixed dates in test case

- test case likey failed due to date rounding

* added error output to console

- temporary, to find out why test fails on mongodb 3.6.9

* create seperate class to avoid multiple geo indices on TestObject class

- mongodb <4.0 does not allow nultiple geo indices on a class when using geoNear
- see https://docs.mongodb.com/v3.6/reference/operator/aggregation/geoNear/#behavior

* fixed incorrect result validation

- results were not ordered properly, so test validation failed sometimes

* removed error output to console

This reverts commit da81c515cbf8cb6edfd82f09ca3087457ac8c727.
This commit is contained in:
Manuel Trezza
2020-03-29 22:15:40 +02:00
committed by GitHub
parent 13bda61fc9
commit 19dea5bbd3
3 changed files with 54 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
const Parse = require('parse/node');
const request = require('../lib/request');
const Config = require('../lib/Config');
const masterKeyHeaders = {
'X-Parse-Application-Id': 'test',
@@ -1428,4 +1429,49 @@ describe('Parse.Query Aggregate testing', () => {
});
}
);
it_only_db('mongo')('geoNear with location query', 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'],
'geoIndex',
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',
query: {
date: {
$gte: new Date(2)
}
}
}
}
];
const query = new Parse.Query(GeoObject);
const results = await query.aggregate(pipeline);
// Check results
expect(results.length).toEqual(2);
expect(results[0].value).toEqual(2);
expect(results[1].value).toEqual(3);
});
});