-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Improved match aggregate #4495
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
Merged
Merged
Improved match aggregate #4495
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,10 @@ const masterKeyOptions = { | |
} | ||
|
||
const loadTestData = () => { | ||
const data1 = {score: 10, name: 'foo', sender: {group: 'A'}, size: ['S', 'M']}; | ||
const data2 = {score: 10, name: 'foo', sender: {group: 'A'}, size: ['M', 'L']}; | ||
const data3 = {score: 10, name: 'bar', sender: {group: 'B'}, size: ['S']}; | ||
const data4 = {score: 20, name: 'dpl', sender: {group: 'B'}, size: ['S']}; | ||
const data1 = {score: 10, name: 'foo', sender: {group: 'A'}, views: 900, size: ['S', 'M']}; | ||
const data2 = {score: 10, name: 'foo', sender: {group: 'A'}, views: 800, size: ['M', 'L']}; | ||
const data3 = {score: 10, name: 'bar', sender: {group: 'B'}, views: 700, size: ['S']}; | ||
const data4 = {score: 20, name: 'dpl', sender: {group: 'B'}, views: 700, size: ['S']}; | ||
const obj1 = new TestObject(data1); | ||
const obj2 = new TestObject(data2); | ||
const obj3 = new TestObject(data3); | ||
|
@@ -252,7 +252,7 @@ describe('Parse.Query Aggregate testing', () => { | |
}).catch(done.fail); | ||
}); | ||
|
||
it('match query', (done) => { | ||
it('match comparison query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
match: { score: { $gt: 15 }}, | ||
|
@@ -266,6 +266,127 @@ describe('Parse.Query Aggregate testing', () => { | |
}).catch(done.fail); | ||
}); | ||
|
||
it('match multiple comparison query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
match: { score: { $gt: 5, $lt: 15 }}, | ||
} | ||
}); | ||
rp.get(Parse.serverURL + '/aggregate/TestObject', options) | ||
.then((resp) => { | ||
expect(resp.results.length).toBe(3); | ||
expect(resp.results[0].score).toBe(10); | ||
expect(resp.results[1].score).toBe(10); | ||
expect(resp.results[2].score).toBe(10); | ||
done(); | ||
}).catch(done.fail); | ||
}); | ||
|
||
it('match complex comparison query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
match: { score: { $gt: 5, $lt: 15 }, views: { $gt: 850, $lt: 1000 }}, | ||
} | ||
}); | ||
rp.get(Parse.serverURL + '/aggregate/TestObject', options) | ||
.then((resp) => { | ||
expect(resp.results.length).toBe(1); | ||
expect(resp.results[0].score).toBe(10); | ||
expect(resp.results[0].views).toBe(900); | ||
done(); | ||
}).catch(done.fail); | ||
}); | ||
|
||
it('match comparison and equality query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
match: { score: { $gt: 5, $lt: 15 }, views: 900}, | ||
} | ||
}); | ||
rp.get(Parse.serverURL + '/aggregate/TestObject', options) | ||
.then((resp) => { | ||
expect(resp.results.length).toBe(1); | ||
expect(resp.results[0].score).toBe(10); | ||
expect(resp.results[0].views).toBe(900); | ||
done(); | ||
}).catch(done.fail); | ||
}); | ||
|
||
it('match $or query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
match: { $or: [{ score: { $gt: 15, $lt: 25 } }, { views: { $gt: 750, $lt: 850 } }]}, | ||
} | ||
}); | ||
rp.get(Parse.serverURL + '/aggregate/TestObject', options) | ||
.then((resp) => { | ||
expect(resp.results.length).toBe(2); | ||
// Match score { $gt: 15, $lt: 25 } | ||
expect(resp.results.some(result => result.score === 20)).toEqual(true); | ||
expect(resp.results.some(result => result.views === 700)).toEqual(true); | ||
|
||
// Match view { $gt: 750, $lt: 850 } | ||
expect(resp.results.some(result => result.score === 10)).toEqual(true); | ||
expect(resp.results.some(result => result.views === 800)).toEqual(true); | ||
done(); | ||
}).catch(done.fail); | ||
}); | ||
|
||
it('match objectId query', (done) => { | ||
const obj1 = new TestObject(); | ||
const obj2 = new TestObject(); | ||
Parse.Object.saveAll([obj1, obj2]).then(() => { | ||
const pipeline = [ | ||
{ match: { objectId: obj1.id } } | ||
]; | ||
const query = new Parse.Query(TestObject); | ||
return query.aggregate(pipeline); | ||
}).then((results) => { | ||
expect(results.length).toEqual(1); | ||
expect(results[0].objectId).toEqual(obj1.id); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('match field query', (done) => { | ||
const obj1 = new TestObject({ name: 'TestObject1'}); | ||
const obj2 = new TestObject({ name: 'TestObject2'}); | ||
Parse.Object.saveAll([obj1, obj2]).then(() => { | ||
const pipeline = [ | ||
{ match: { name: 'TestObject1' } } | ||
]; | ||
const query = new Parse.Query(TestObject); | ||
return query.aggregate(pipeline); | ||
}).then((results) => { | ||
expect(results.length).toEqual(1); | ||
expect(results[0].objectId).toEqual(obj1.id); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('match pointer query', (done) => { | ||
const pointer1 = new TestObject(); | ||
const pointer2 = new TestObject(); | ||
const obj1 = new TestObject({ pointer: pointer1 }); | ||
const obj2 = new TestObject({ pointer: pointer2 }); | ||
const obj3 = new TestObject({ pointer: pointer1 }); | ||
|
||
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3]).then(() => { | ||
const pipeline = [ | ||
{ match: { pointer: pointer1.id } } | ||
]; | ||
const query = new Parse.Query(TestObject); | ||
return query.aggregate(pipeline); | ||
}).then((results) => { | ||
expect(results.length).toEqual(2); | ||
expect(results[0].pointer.objectId).toEqual(pointer1.id); | ||
expect(results[1].pointer.objectId).toEqual(pointer1.id); | ||
expect(results.some(result => result.objectId === obj1.id)).toEqual(true); | ||
expect(results.some(result => result.objectId === obj3.id)).toEqual(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you can just compare that the expected objectId matches directly |
||
done(); | ||
}); | ||
}); | ||
|
||
it('project query', (done) => { | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just check that the value is equal to the expected value directly, rather than an equality check and bool check.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mongo and PG return different orders for results
If I check directly, one of the test would fail even tho they return correct results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case wouldn't this fail anyways with a comparison against a different result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.some()
returns true if at least one element matches. So the order doesn’t matter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also helps if future databases changes matches internally or query optimization returns different results order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, gotcha, alright we're good then.