Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue on count with Geo constraints and mongo (issue #5285) #5286

Merged
merged 4 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions spec/ParseGeoPoint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ describe('Parse.GeoPoint testing', () => {
obj.set('location', point);
obj.set('name', 'Ferndale');
await obj.save();
const results = await new Parse.Query(TestObject).find();
equal(results.length, 1);
const pointAgain = results[0].get('location');
const result = await new Parse.Query(TestObject).get(obj.id);
const pointAgain = result.get('location');
ok(pointAgain);
equal(pointAgain.latitude, 44.0);
equal(pointAgain.longitude, -11.0);
Expand Down Expand Up @@ -727,4 +726,49 @@ describe('Parse.GeoPoint testing', () => {
done();
});
});

it('withinKilometers supports count', async () => {
const inside = new Parse.GeoPoint(10, 10);
const outside = new Parse.GeoPoint(20, 20);

const obj1 = new Parse.Object('TestObject', { location: inside });
const obj2 = new Parse.Object('TestObject', { location: outside });

await Parse.Object.saveAll([obj1, obj2]);

const q = new Parse.Query(TestObject).withinKilometers(
'location',
inside,
5
);
const count = await q.count();

equal(count, 1);
});

it('withinKilometers complex supports count', async () => {
const inside = new Parse.GeoPoint(10, 10);
const middle = new Parse.GeoPoint(20, 20);
const outside = new Parse.GeoPoint(30, 30);
const obj1 = new Parse.Object('TestObject', { location: inside });
const obj2 = new Parse.Object('TestObject', { location: middle });
const obj3 = new Parse.Object('TestObject', { location: outside });

await Parse.Object.saveAll([obj1, obj2, obj3]);

const q1 = new Parse.Query(TestObject).withinKilometers(
'location',
inside,
5
);
const q2 = new Parse.Query(TestObject).withinKilometers(
'location',
middle,
5
);
const query = Parse.Query.or(q1, q2);
const count = await query.count();

equal(count, 2);
});
});
6 changes: 3 additions & 3 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ export class MongoStorageAdapter implements StorageAdapter {
deleteAllClasses(fast: boolean) {
return storageAdapterAllCollections(this).then(collections =>
Promise.all(
collections.map(
collection => (fast ? collection.deleteMany({}) : collection.drop())
collections.map(collection =>
fast ? collection.deleteMany({}) : collection.drop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change covered in a test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier making the code look good

)
)
);
Expand Down Expand Up @@ -693,7 +693,7 @@ export class MongoStorageAdapter implements StorageAdapter {
readPreference = this._parseReadPreference(readPreference);
return this._adaptiveCollection(className)
.then(collection =>
collection.count(transformWhere(className, query, schema), {
collection.count(transformWhere(className, query, schema, true), {
maxTimeMS: this._maxTimeMS,
readPreference,
})
Expand Down
37 changes: 25 additions & 12 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const valueAsDate = value => {
return false;
};

function transformQueryKeyValue(className, key, value, schema) {
function transformQueryKeyValue(className, key, value, schema, count = false) {
switch (key) {
case 'createdAt':
if (valueAsDate(value)) {
Expand Down Expand Up @@ -293,7 +293,7 @@ function transformQueryKeyValue(className, key, value, schema) {
return {
key: key,
value: value.map(subQuery =>
transformWhere(className, subQuery, schema)
transformWhere(className, subQuery, schema, count)
),
};
case 'lastUsed':
Expand Down Expand Up @@ -330,7 +330,7 @@ function transformQueryKeyValue(className, key, value, schema) {
}

// Handle query constraints
const transformedConstraint = transformConstraint(value, field);
const transformedConstraint = transformConstraint(value, field, count);
if (transformedConstraint !== CannotTransform) {
if (transformedConstraint.$text) {
return { key: '$text', value: transformedConstraint.$text };
Expand Down Expand Up @@ -359,14 +359,15 @@ function transformQueryKeyValue(className, key, value, schema) {
// Main exposed method to help run queries.
// restWhere is the "where" clause in REST API form.
// Returns the mongo form of the query.
function transformWhere(className, restWhere, schema) {
function transformWhere(className, restWhere, schema, count = false) {
const mongoWhere = {};
for (const restKey in restWhere) {
const out = transformQueryKeyValue(
className,
restKey,
restWhere[restKey],
schema
schema,
count
);
mongoWhere[out.key] = out.value;
}
Expand Down Expand Up @@ -816,7 +817,7 @@ function relativeTimeToDate(text, now = new Date()) {
// If it is not a valid constraint but it could be a valid something
// else, return CannotTransform.
// inArray is whether this is an array field.
function transformConstraint(constraint, field) {
function transformConstraint(constraint, field, count = false) {
const inArray = field && field.type && field.type === 'Array';
if (typeof constraint !== 'object' || !constraint) {
return CannotTransform;
Expand Down Expand Up @@ -1002,15 +1003,27 @@ function transformConstraint(constraint, field) {
}
break;
}
case '$nearSphere':
var point = constraint[key];
answer[key] = [point.longitude, point.latitude];
case '$nearSphere': {
const point = constraint[key];
if (count) {
answer.$geoWithin = {
$centerSphere: [
[point.longitude, point.latitude],
constraint.$maxDistance,
],
};
} else {
answer[key] = [point.longitude, point.latitude];
}
break;

case '$maxDistance':
}
case '$maxDistance': {
if (count) {
break;
}
answer[key] = constraint[key];
break;

}
// The SDKs don't seem to use these but they are documented in the
// REST API docs.
case '$maxDistanceInRadians':
Expand Down