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

Question: Query db by MongoDate #51

Closed
milosh012 opened this issue Oct 10, 2013 · 13 comments
Closed

Question: Query db by MongoDate #51

milosh012 opened this issue Oct 10, 2013 · 13 comments

Comments

@milosh012
Copy link

Hello,

first want to say that your extension is awesome!

I have one question... I have mongodb collection saved with models that have column type "Date". When I access that collection I see that column with Date type is actually MongoDate object.

My question is how I can query by that column? I have tried next scenarios with no success:

DB::connection('mongodb')->collection('names')->where('date', '<', new MongoDate())
DB::connection('mongodb')->collection('names')->where('date', '<', new DateTime())
DB::connection('mongodb')->collection('names')->where('date', '<', time())
DB::connection('mongodb')->collection('names')->where('date', '<', date('Y-m-d'))

Thanks,
Milos

@jenssegers
Copy link
Contributor

For me, MongoDate() seems to be working. But new DateTime would be better I think.

@devbeans
Copy link

devbeans commented Nov 7, 2013

I am also having a problem with the dates, I have a script like this:

$from = new MongoDate(Input::get('from')); //from is in time format
$to = new MongoDate(Input::get('to')); // to is in time format
$between = array( $from, $to);
$leads = Lead::whereBetween('created_at', $between)->get();

I don't know what I'm missing but the date range is not working, is there something I am missing? Thanks for writing a great extension btw.

Cheers,
Albert

@jenssegers
Copy link
Contributor

I'm looking into this right now.

jenssegers added a commit that referenced this issue Nov 7, 2013
@jenssegers
Copy link
Contributor

I did not encounter any problems with dates. Check the date tests in 93e53ec

@devbeans
Copy link

devbeans commented Nov 7, 2013

Awesome, got some tips from your tests. Thank you

@jenssegers
Copy link
Contributor

Added some more date tweaks and a bit of documentation: https://github.com/jenssegers/Laravel-MongoDB#dates

@jenssegers
Copy link
Contributor

I added Carbon date support to the latest version. Can anyone report back if he's having problems with these changes?

@sebastiaanluca
Copy link

Could you perhaps quickly explain how to get MongoDate objects converted to Carbon instances? Whenever I read out data, the created_at and updated_at fields are always of the MongoDate type.

@atapatel
Copy link

atapatel commented Oct 9, 2015

I am facing the issue with new version..

@jkpatil211
Copy link

@sebastiaanluca : u can use MongoDate::toDateTime as given in following link
http://php.net/manual/en/mongodate.todatetime.php

@saraht129
Copy link

@jenssegers The whereBetween query doesn't work with Carbon date, only MongoDate. Using v2.2.4.

    $startDate = Carbon::createFromDate(2015, 4, 1);
    $endDate = Carbon::createFromDate(2015, 4, 30);
    $users = User::whereBetween('created_at', array($startDate, $endDate))->get();
    //Return empty result

    $startDate = new MongoDate(Carbon::createFromDate(2015, 4, 1)->timestamp);
    $endDate = new MongoDate(Carbon::createFromDate(2015, 4, 30)->timestamp);
    $users = User::whereBetween('created_at', array($startDate, $endDate))->get();
    //Return correct results

Is the Carbon date support not applicable to this case? Thanks!

@thiagoalves-dev
Copy link

I am facing the issue with new version..²

@mshahamirian
Copy link

@saraht129 @jenssegers I've tired your solution and still get empty result! Also tried (for example)
User::where('created_at','>',$startDate)->where('created_at','<',$endDate)->get();
still get empty result.
I don't know why it doesn't work with more than one where clause 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants