Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

PHPLIB-1340 Remove Projection Operators #54

Merged
merged 3 commits into from
Jan 31, 2024
Merged

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jan 24, 2024

Fix PHPLIB-1340

The projection operators can only be used with the find command.

https://www.mongodb.com/docs/manual/reference/operator/query/#projection-operators

Also removed $filter from Projection operators, as it's an Expression operator.

@GromNaN GromNaN requested a review from jmikola January 24, 2024 16:41
@GromNaN GromNaN force-pushed the PHPLIB-1340 branch 2 times, most recently from 073eaf3 to b1300df Compare January 26, 2024 10:29
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

I see you crossed out the positional operator ($). Is there no work to be done for that?

comments:
# Example uses the short form, the builder always generated the verbose form
# $slice: 3
$slice: [3]
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem correct, as the docs indicate that the array form requires both elements:

Specifies the number of elements to return in the after skipping the specified number of elements starting from the first element. You must specify both elements.

Also, the short form is $slice: <number to return>. When an array form is used, the first value specifies the number to skip and <number to return> is the second value.

I would hope the server would just error on this since it's ambiguous, although I could see a case where is accepted and allows $slice to skip a few elements and return everything else. The server docs don't suggest that's possible, so I assume you can only accomplish that by specifying a very large <number to return> and hoping it exceeds the array size.

Copy link
Member Author

@GromNaN GromNaN Jan 26, 2024

Choose a reason for hiding this comment

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

Looks like we will need a custom encoder.

$slice: [3] returns a server error.

MongoServerError: Invalid $slice syntax. The given syntax { $slice: [ 3 ] } did not match the find() syntax because :: Location31272: $slice array argument should be of form [skip, limit] :: The given syntax did not match the expression $slice syntax. :: caused by :: Expression $slice takes at least 2 arguments, and at most 3, but 1 were passed in.

Note: the version with 3 arguments is not documented.

Copy link
Member

Choose a reason for hiding this comment

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

the version with 3 arguments is not documented

I apologize for the headache, but that sounds like it warrants a DOCSP ticket. And make sure to tell them you shouldn't be used as the technical reviewer when/if it actually gets implemented (this frequently happens to me when I report server docs issues). It should really be someone from server, since they'll have to confirm what it actually does.

And if it should remain undocumented, server should consider revising this error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

{
$pipeline = new Pipeline(
Stage::project(
comments: Projection::slice(-1, 3),
Copy link
Member

Choose a reason for hiding this comment

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

I think the argument order for Projection::slice will need to change, and I can see this being confusing for users since it will differ from the server API.

Both the short and long forms accept a <number to return> value, and the long form also accepts <number to skip>. If the PHP API is going to provide a consistent API without using named parameters, <number to skip> should be an optional second parameter.

@GromNaN
Copy link
Member Author

GromNaN commented Jan 26, 2024

I see you crossed out the positional operator ($). Is there no work to be done for that?

Maybe something similar to fieldpath then?

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Since the positional operator can occur anywhere within a field path, I'm not certain a separate builder method is necessary unless you intend to enforce some typing with it and assert that it's well formed (such as ".$" at the end of a path or ".$." within a path).

I'm not convinced that's needed, though.

@GromNaN GromNaN changed the title PHPLIB-1340 Add tests on Projection Operators PHPLIB-1340 Remove Projection Operators Jan 30, 2024
@GromNaN
Copy link
Member Author

GromNaN commented Jan 30, 2024

I decided to remove all projection operators. They can't be used in $project stage as I was expecting. They are dedicated to the find command. The $slice must be used as an expression (with the array as 1st parameter) and $elemMatch is replaced by $filter.

@GromNaN GromNaN requested review from jmikola and alcaeus January 30, 2024 16:04
@GromNaN GromNaN merged commit edd6a1c into mongodb:0.1 Jan 31, 2024
@GromNaN GromNaN deleted the PHPLIB-1340 branch January 31, 2024 10:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants