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

Add pipeline to $lookup stage #2459

Closed
wants to merge 18 commits into from
Closed

Add pipeline to $lookup stage #2459

wants to merge 18 commits into from

Conversation

khaperets
Copy link
Contributor

Q A
Type feature
BC Break yes
Fixed issues #1863

Summary

Added a pipeline lookup functionality that helps a lot in the futures developments,

@Ravetracer
Copy link

I discovered that "foreignField" and "localField" is not mandatory when using "pipeline" in the $lookup stage. In fact it dropped the performance in our case so we had a query running for about 4 seconds. Without foreignField and localField the query runs in 250-300ms and the result set was the same.
Also it would be great if you can add an option to set variables using "let".

@khaperets
Copy link
Contributor Author

@Ravetracer Thanks for your comment. Changes pushed to PR.

@malarzm malarzm added this to the 2.5.0 milestone Sep 18, 2022
@malarzm
Copy link
Member

malarzm commented Sep 18, 2022

@khaperets thanks for the PR! It seems that the test you've added is failing: https://github.com/doctrine/mongodb-odm/actions/runs/3067090011/jobs/4971970883#step:12:1783. The rest of errors seems to be unrelated, we'll investigate them. Once the test is green please fix coding standards, this can be done by running vendor/bin/phpcbf command. If possible, please also squash your commits so there's just one.

@khaperets
Copy link
Contributor Author

@malarzm I've done everything, thank you for the feedback.

@malarzm
Copy link
Member

malarzm commented Sep 20, 2022

@khaperets we've managed to get checks back to shape. May I ask you to rebase your PR atop newly created 2.5.x branch?

@malarzm malarzm changed the base branch from 2.4.x to 2.5.x September 20, 2022 22:21
franmomu and others added 7 commits September 21, 2022 00:26
@khaperets
Copy link
Contributor Author

@malarzm done

if (! empty($this->let)) {
$expression['$lookup']['let'] = $this->let;
}
} else {
Copy link
Member

@malarzm malarzm Sep 20, 2022

Choose a reason for hiding this comment

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

According to https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#correlated-subqueries-using-concise-syntax MongoDB 5.0 allows using all 4 fields together so the current these-or-those approach doesn't seem like a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Add pipeline to $lookup stage
Add pipeline to $lookup stage
@khaperets
Copy link
Contributor Author

@malarzm Added compatibility with MongoDB 5.0

'as' => $this->as,
],
];

$serverVersion = $this->dm->getServerVersion($this->class->getName());
$isSupportsPipelineWithLocalField = version_compare($serverVersion, '5.0') >= 0;
Copy link
Member

Choose a reason for hiding this comment

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

Generally we're letting end users handle version-specific behaviour as they do know what MongoDB version they're running. The most important reason is to prevent additional communication with database, as introduced here. So instead I'd rather see a way to reset builder's localField and foreignField properties when adding pipeline/let. I think the cleanest way to do will be a new method resetLocalAndForeignField that will unset both properties. Additionally this will allow use cases like #2459 (comment) to control how they query will be ran.

Also I didn't manage to comment last time, but I believe I've seen the test fail due to MongoDB version. Once we get back to letting users control the behaviour, you can skip some tests using methods like \Doctrine\ODM\MongoDB\Tests\BaseTest::requireMongoDB42.

Also feel free to ask me anything on our Slack, that way I'll be able to help faster :)

@malarzm
Copy link
Member

malarzm commented Oct 2, 2022

Merged manually in 7ebc038. Thanks a lot @khaperets!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants