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

Using limit() in count queries is ignored #8811

Open
4 tasks done
hej2010 opened this issue Nov 17, 2023 · 9 comments · May be fixed by #8812
Open
4 tasks done

Using limit() in count queries is ignored #8811

hej2010 opened this issue Nov 17, 2023 · 9 comments · May be fixed by #8812
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@hej2010
Copy link
Contributor

hej2010 commented Nov 17, 2023

New Issue Checklist

Issue Description

Using .limit(X) in a count() query is ignored.

Steps to reproduce

Run a query with .limit(X) on a collection.

Actual Outcome

The count result is the number of rows matching the query, which can be greater than the specified limit.

Expected Outcome

The count is the smallest of the number of rows and the X in .limit(X).

Environment

Server

  • Parse Server version: 6.5.0-beta.1
  • Operating system: Ubuntu 22
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 5.3.2
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): local

Logs

Copy link

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

@matheusfrozzi
Copy link

I think that's how it's supposed to work, count the total of the query, to you know how many pages with skip/limit you can navigate.
In your case, you already know how much would be the count, the same as the limit you asked for.. and if is less, the current count would bring the correct count.

@hej2010
Copy link
Contributor Author

hej2010 commented Nov 17, 2023

I think that's how it's supposed to work, count the total of the query, to you know how many pages with skip/limit you can navigate. In your case, you already know how much would be the count, the same as the limit you asked for.. and if is less, the current count would bring the correct count.

It can still count 100k+ rows for my use case (counting a user's unread messages) when I try to limit it to 100, it's a bit unnecessary.

@mtrezza
Copy link
Member

mtrezza commented Nov 17, 2023

The MongoDB count operation supports the limit parameter, see docs. It may be more efficient than a db.collection.find(query).limit(number).count();. So I assume supporting the limit param has a use case.

@matheusfrozzi
Copy link

but if you put a limit of 100, what do you expect to be the count result?

@mtrezza mtrezza added type:feature New feature or improvement of existing feature type:bug Impaired feature or lacking behavior that is likely assumed and removed type:feature New feature or improvement of existing feature labels Nov 17, 2023
@hej2010
Copy link
Contributor Author

hej2010 commented Nov 17, 2023

A value between 0 and 100 :)

@matheusfrozzi
Copy link

I see, but as I said before, the count is to know how many times you can page it.
For example: 140 articles with a limit of 10, 14 pages.
the way you want, you don't need to count
Just get the return array and ask for the length.

@radandevist
Copy link

I agree with @matheusfrozzi here. Altering the current behaviour of the count() method makes no sense.
Just get the length of the query's returned array for your use case.
If you want to change anything regarding that anyway, I think it's better to add a new method limitCount() for example or add an optional property to the count method: count({ withinLimit: true }).

@mtrezza
Copy link
Member

mtrezza commented May 15, 2024

This is primarily a syntax question. Whether there are use cases for it is a different topic. I believe the expected behavior of Parse.Query.limit(n).count() would be that it respects limit(n). Ignoring limit(n) would be syntactically counterintuitive.

There is of course a practical aspect to it. If you want to know whether a collection of N documents contains less than or equal to n documents matching a filter, then a database engine optimization may stop the query cursor once it has found n documents, instead of going through all N documents.

In fact, MongoDB hints towards that:

cursor.limit says:

Use limit() to maximize performance and prevent MongoDB from returning more results than required for processing.

cursor.count also provides a specific example:

db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).limit(5).count(true)

Since MongoDB explicitly supports the optional limit parameter for count operations, we can assume that the engine does optimize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants