-
-
Notifications
You must be signed in to change notification settings - Fork 342
How can I query dateTime field by Month and Year only #331
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
Comments
Hey @itsursujit. Regarding querying via date time unless you know the exact date you want using What you can do to get the range is set an upper and lower bound in your query to return all objects created within a given date range. $query = new ParseQuery("ParseClass");
// lower bound
$query->greaterThan(
"createdAt",
DateTime::createWithFormat("Y-m-d H:i:s", "2017-05-01 00:00:00")
);
// upper bound
$query->lessThan(
"createdAt",
DateTime::createWithFormat("Y-m-d H:i:s", "2017-06-01 00:00:00")
);
// find all objects that lie between
// the start of the 1st of May
// and start of the 1st of June
// non-inclusive
$objectsWithinDates = $query->find(); If you need the queries to return results from a given set date (inclusive) you can use |
Thank you for the reply. The above query is getting the object list within a month. What if I needed to get the length of objects by list of ranging... just like SQL Queries with GROUP BY |
You can use // sort by ascending date
$query->ascending("createdAt");
// sort by descending date
$query->descending("createdAt"); There's some pretty good documentation on this and more in the query constraints section of the php sdk docs as well. ::EDIT:: @itsursujit Didn't realize that was not an You have a couple options however, the first being you can sort through the data yourself and group by month by checking each date. The second being you could query for each individual month of results one at a time, that way the only results you get back are those within a singular month. |
@itsursujit were you able to figure out your issue? |
Hey Benjamin, Thank you for your response. I tried it manually. But manual grouping and adding is not an option. In future can we have the functionality in parse? |
@itsursujit I would recommend a beforeSave / afterSave that takes the createdAt and converts it to a string with Month and Year that you can query with equalTo. As far as groupBy you can use Json/Dictionary keys |
As dplewis has pointed out, there's more than one way to do this, you should definitely be able to make this work without having it built in. Essentially, similar to what's mentioned above, you could simply be fetching everything within a range and grouping by Year and Month alone. // setting up the query beforehand...
$sorted = []
$objs = $query->find();
foreach($objs as $obj) {
$dateString = $obj->getCreatedAt()->format('m-Y');
if(!isset($sorted[$dateString])) {
$sorted[$dateString] = [];
}
$sorted[$dateString][] = $obj;
}
// voila! handle your monthly sorted objects as you wish If you still feel that this will not suffice you should head over to the parse-server repo and open an issue requesting functionality like Although I would encourage you to see if you can make this or any of the above solutions (or any other unnamed approaches) work first before you consider that. |
I'm using the same approach i.e. finding and sorting manually using |
@spbaniya that's good to know that there are others who would be interested in this. After taking a brief look in the parse-server repo the closest prior issue I found was #2238, where In the meantime...I have a thought. Perhaps until such functionality is available server side we could setup When the functionality is implemented server side we could alter the behavior of As for additional aggregation (besides Again, this is just a thought. What does everyone think here of it? |
I like the idea of smarter date queries, with YY-mm, YY-mm–dd patterns as well as ‘1d’ ago etc... we’ll be working in that area soon as it’s something we’ll require in our push schedule work. |
@marvelm, you may wanna follow up here :) |
So just to make sure this doesn't go by the wayside @flovilmart is there an issue or related PR opened for such functionality that we could mention? Or, if this tentative, perhaps we can open up the such an issue just so it's brought up over at the server. ::EDIT:: |
There’s no issue open at the moment, we’re in the process internally and we’ll open the relevant issue when we have better identified all the needs :) |
Just to note #4289 over at parse-server is currently pending and proposes to introduce relative time based queries. Assuming this is completed and merged in we'll be adding the relative query functionality to @itsursujit I believe this would be the functionality you were originally looking for 👍 . |
@montymxb Thank you for sticking with this functionality. I'll check and let you know... |
parse-server release 2.6.5 now has relative time queries 👍 ! I have put together a project for a planned 1.4.0 release. This is more or less a list of the desired features to be implemented before we move up to the next major version. Depending on how people are tracking this (by releases for example) we may want to bump to something minor like 1.3.1 with just the changes for relative time queries, and perhaps one or two other things. That way everyone can get the new features a bit sooner. I'll most likely make the addition of relative queries some time this weekend. Everyone is also welcome to take a stab at it and put up a PR sometime sooner. On top of that there's also plenty of other potential PRs in the project above ^. If anyone is still looking to get their hacktoberfest shirts 🎃 (and their name in our next release) feel free to start work on any of those features, or open up an issue relating to that feature with any questions you guys have about getting started! Other than that I'll keep this issue posted about the progress for relative time queries. |
#360 has been added with the ability to make relative time queries. Details on the usage can be found in the PR. I'll be following up with an additional PR to incorporate details into our docs/README on this page, which will be before we run towards a 1.4.0 release. Although this functionality is merged in it won't be available until the next release. This may wait until 1.4.0 or will be pushed out as a minor 1.3.1 release, haven't decided yet on that. |
@itsursujit @spbaniya we've recently merged in aggregate functionality (#355) for a pending 1.4.0 release. This allows for such behavior as would be expected from GROUP BY. However I haven't quite checked if we have support in place for grouping by date, like 'Y-m'. If not we'll probably have something like that coming up in a bit. |
Here's an example of counting new user creation grouped by day using parse-server aggregate:
I'm going to close this. |
Hello,
I'm writing this on issues since I didn't find any other platform to look for my problem. I was trying to query the recordset based on the DateTime field in terms of Month and Year.
For e.g. I needed like
$query = new ParseQuery("ParseClass");
$query->equalsTo("createdAt", "2017-05");
$parseObject = $query->find();
The text was updated successfully, but these errors were encountered: