Skip to content

PHPLIB-417: countDocuments should use group with _id: 1 #652

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

Closed
wants to merge 0 commits into from

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Jul 29, 2019

@alcaeus alcaeus requested a review from kvwalker July 29, 2019 09:27
@alcaeus alcaeus self-assigned this Jul 29, 2019
@lindelius
Copy link

Isn't this just going to return either 1 or 0, though?

@alcaeus
Copy link
Member Author

alcaeus commented Jul 29, 2019

Isn't this just going to return either 1 or 0, though?

The countDocuments helper doesn't rely on collection metadata to return a document count, but rather runs an aggregation pipeline containing a $group pipeline stage to count the documents. In this case, we're grouping by the value 1, which will cause the $group stage to emit a single document with the n field containing the number of documents that were emitted from the previous $match, $skip, and $limit stages.

@lindelius
Copy link

lindelius commented Jul 29, 2019

Is the ”output a single document” functionality being changed to 1 from null? The shell documentation is still using null in the examples and we’re using the same in quite a few places that’ll have to be changed too if that’s the case

@alcaeus
Copy link
Member Author

alcaeus commented Jul 29, 2019

The motivation for the change is explained in mongodb/specifications#504:

The $group stage of an aggregation pipeline can use any constant to achieve the group-for-all effect. Switching to using 1 as a constant, to avoid test runner issues associated with null values

This has no implications on the result or on whether you should use null or 1 when doing such a $group aggregation yourself. In our case the change is necessary to pass the spec tests once adding support for retryable reads.

The "output a single document" functionality is not tied to anything: this is a side effect of using $group with a constant value for _id, no matter what this constant value is. You might as well use _id: 'The quick brown fox jumps over the lazy dog'.

@lindelius
Copy link

lindelius commented Jul 29, 2019

Ok! Thanks for clarifying. I was under the impression that this was only for when setting the _id field to null.

It’s propbably not your table, but it would be useful to update the documentations to read ”constant value” rather than ”null” for the examples etc.

@alcaeus
Copy link
Member Author

alcaeus commented Jul 29, 2019

It’s propbably not your table, but it would be useful to update the documentations to read ”constant value” rather than ”null” for the examples etc.

I don't know which table you are referring to. A quick search led me to the documentation for $group, which states:

The _id field is mandatory; however, you can specify an _id value of null, or any other constant value, to calculate accumulated values for all the input documents as a whole.

Copy link
Contributor

@kvwalker kvwalker left a comment

Choose a reason for hiding this comment

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

looks good to me!

@jmikola
Copy link
Member

jmikola commented Jul 30, 2019

@lindelius: Nothing to worry about on your end (or any other application). For purposes of $group, any constant value (provided it's not some special operator/expression) should provide the same effect of grouping into a single accumulation bucket.

The impetus for this change was that several internal specification tests had trouble asserting outgoing commands because those same tests often use null to signify that a field should not exist in the document, rather than asserting that it contains a BSON null value.

It’s propbably not your table, but it would be useful to update the documentations to read ”constant value” rather than ”null” for the examples etc.

@alcaeus linked to the current documentation, but for reference this change was made in mongodb/docs#3542 in the manual for server versions 4.0+. I'm not sure if it was back-ported, but it's possible you were looking at $group documentation from an older version. If there's another instance in the 4.0+ docs that we missed, please let us know, though (or use the edit link in the server manual to submit a PR!).

alcaeus added a commit to alcaeus/mongo-php-library that referenced this pull request Jul 30, 2019
@alcaeus alcaeus closed this Jul 30, 2019
@alcaeus alcaeus deleted the phplib-417 branch July 30, 2019 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants