laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Fix Query on whereDate, whereDay, whereMonth, whereYear

Open Davpyu opened this issue 3 years ago • 3 comments

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:

  1. whereTime isnt possible to fix rn since i cant find logic to query like time() on sql

Davpyu avatar Mar 28 '22 13:03 Davpyu

Any update for this one?

Davpyu avatar Jul 13 '22 04:07 Davpyu

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!

divine avatar Jul 13 '22 11:07 divine

Okay i'll update after working hours

Davpyu avatar Jul 28 '22 04:07 Davpyu

Hello @Davpyu,

Can you please update or I should take care of it?

Thanks!

divine avatar Nov 04 '22 12:11 divine

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

Davpyu avatar Nov 16 '22 09:11 Davpyu

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

Davpyu avatar Jan 18 '23 09:01 Davpyu

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.

whereTime isnt possible to fix rn since i cant find logic to query like time() on sql

I found a solution using $dateToString.

GromNaN avatar Aug 22 '23 10:08 GromNaN

Closing in favour of #2572.

alcaeus avatar Aug 22 '23 11:08 alcaeus