Parse-server: Aggregate cannot correctly aggregate date fields

Created on 5 Jun 2020  路  5Comments  路  Source: parse-community/parse-server

Issue Description

I need to count the new data every day

group: {
    objectId: { day: { $dayOfMonth: "$createdAt" }, month: { $month: "$createdAt" }, year: { $year: "$createdAt" } },
    count: { $sum: 1 },
},

Result:
image

Expected Results

Count the same date together

Environment Setup

  • Server

    • parse-server version (Be specific! Don't say 'latest'.) : 3.0.8&4.2.0
    • Operating System: ubuntu
  • Database
    Postgresql 10

bug

All 5 comments

Sorry, I didn't find detailed documentation to explain the usage of aggregation, it really behaves like a bug

@nothinggift Sorry, I just realized your question was for Postgres, not MongoDB.

My Postgres knowledge is limited, but what you can try is to query the database directly without Parse Server to verify that your syntax is correct. If you don't get the results you expect directly from the database, then you may want to open a question on Stack Overflow to get help with formulating your query. You can then compare the query that works with the query that Parse Server does against the database. If you cannot get the same query via Parse Server, you can reopen the issue here for someone to look into that.

In the Postgres Storage Adapter you can see how your group stage is transformed into a Postgres query, maybe that will give you some insight:
https://github.com/parse-community/parse-server/blob/d0a9c709fea4cbdba540420353a36a0e89601ffb/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js#L2232-L2315

For reference, in MongoDB you would do it like this, but that is specific to MongoDB:

let pipeline = [{
    group: {
        objectId: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
        count: {
            $sum: 1
        }
    }
}];

I didn't find detailed documentation to explain the usage of aggregation

I agree that the docs for aggregate lack an example for date aggregation, which may be a frequent use case. Maybe you want to add an example there after you got the query working and open a PR.

I checked the source code, this is indeed a bug, no matter how you write "objectId", the groupBy key is a combination of the original fields, which means that in this example it will always be "group by createdAt"

Thanks for pointing this out. To clarify, can you write what the expected vs. the currently generated Postgres query is? Do you think you can open a PR to fix this?

Ok i will try.

Was this page helpful?
0 / 5 - 0 ratings