laravel-mongodb
laravel-mongodb copied to clipboard
Fix Query on whereDate, whereDay, whereMonth, whereYear
Fix issues #2334
So basically, current whereDate, whereDay, whereMonth and whereYear is query on different column with specific need (like whereDate you need column that has value YYYY-MM-DD etc) using basic comparison.
As we all know that isnt how whereDate works on sql, this PR will generate query using built in $dayOfMonth, $month, $year on whereDay, whereMonth, and whereYear, while whereDate basically generate range date(since mongodb doesnt have date() equivalent like on sql).
Example: Collection on mongodb
db.birthdays.insert([
{ '_id': 1, 'name': 'John Doe', 'birthday_at': new Date('2002-02-01') },
{ '_id': 2, 'name': 'Nash Doe', 'birthday_at': new Date('1992-06-20') },
{ '_id': 3, 'name': 'Jane Doe', 'birthday_at': new Date('2000-01-15') },
]);
Eloquent if this merged
Birthday::whereDate('birthday_at', '2002-02-01')->get();
// generated query: birthdays.find({ 'birthday_at' => { '$gte' => '2002-02-01T00:00:00.000Z', '$lte' => '2002-02-01T23:59:59.999Z' } })
Birthday::whereDay('birthday_at', 15)->get();
// generated query: birthdays.find({ '$expr' => { '$eq' => [{ '$dayOfMonth' => 'birthday_at' }, 15] } })
Birthday::whereMonth('birthday_at', 6)->get();
// generated query: birthdays.find({ '$expr' => { '$eq' => [{ '$month' => 'birthday_at' }, 6] } })
Birthday::whereYear('birthday_at', 2002)->get();
// generated query: birthdays.find({ '$expr' => { '$eq' => [{ '$year' => 'birthday_at' }, 2002] } })
Note:
- whereTime isnt possible to fix rn since i cant find logic to query like time() on sql
Any update for this one?
Any update for this one?
Hello,
Please remove styleci changes as this isn't related to this PR.
I don't see any check runs for this PR?
Thanks!
Okay i'll update after working hours
Hello @Davpyu,
Can you please update or I should take care of it?
Thanks!
Hello @Davpyu,
Can you please update or I should take care of it?
Thanks!
im sorry for not responding so long because busy with work, yes you can take care of this PR if you want it
Any update for this one?
Hello,
Please remove styleci changes as this isn't related to this PR.
I don't see any check runs for this PR?
Thanks!
Hello, i've reverted the styleci for this PR. Thanks
Hello @Davpyu, thanks for your work on this feature. I'm taking over your PR in #2572 to get it merged in the next release.
whereTimeisnt possible to fix rn since i cant find logic to query like time() on sql
I found a solution using $dateToString.
Closing in favour of #2572.