Skip to content

Question: Query db by MongoDate #51

Closed
@milosh012

Description

@milosh012

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

Activity

jenssegers

jenssegers commented on Oct 14, 2013

@jenssegers
Contributor

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

devbeans

devbeans commented on Nov 7, 2013

@devbeans

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

jenssegers commented on Nov 7, 2013

@jenssegers
Contributor

I'm looking into this right now.

added a commit that references this issue on Nov 7, 2013
jenssegers

jenssegers commented on Nov 7, 2013

@jenssegers
Contributor

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

devbeans

devbeans commented on Nov 7, 2013

@devbeans

Awesome, got some tips from your tests. Thank you

jenssegers

jenssegers commented on Nov 7, 2013

@jenssegers
Contributor

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

jenssegers

jenssegers commented on Nov 8, 2013

@jenssegers
Contributor

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

sebastiaanluca

sebastiaanluca commented on Jan 23, 2014

@sebastiaanluca

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

atapatel commented on Oct 9, 2015

@atapatel

I am facing the issue with new version..

jkpatil211

jkpatil211 commented on Oct 27, 2015

@jkpatil211

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

saraht129

saraht129 commented on Jan 29, 2016

@saraht129

@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

thiagoalves-dev commented on Aug 1, 2016

@thiagoalves-dev

I am facing the issue with new version..²

mshahamirian

mshahamirian commented on May 22, 2017

@mshahamirian

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @devbeans@jenssegers@sebastiaanluca@milosh012@mshahamirian

        Issue actions

          Question: Query db by MongoDate · Issue #51 · mongodb/laravel-mongodb