skip to Main Content

Hello I’m new in MongoDB laravel and I have the following error:

This is the object I want to query in Mongo.

MongoDB Compass

I want to fetch timestamps that are greater than or equal to 2022/7/5 and less than or equal to 2022/7/6, according to my logic in Laravel with the Jenssegers/laravel-mongodb, would have this:

$tracks =  TrackFishing::where('boat_id', '3')
    ->whereBetween(
         'tracking.timestamp', array(
             Carbon::createFromDate(2022, 7, 5),
             Carbon::createFromDate(2022, 7, 6)
         ))
    ->first();

I want to get position 0 of the timestamp but this throws me empty, I’d appreciate your help

3

Answers


  1. Chosen as BEST ANSWER

    I solved with a aggregate:

    $cursor = Model::raw()->aggregate([
            ['$match' => [
                    'tracking.timestamp' => [
                        '$gte' => new MongoDBBSONUTCDateTime(Carbon::createFromFormat("Y-m-d", $request->fecha_inicio)->startOfDay()),
                        '$lte' => new MongoDBBSONUTCDateTime(Carbon::createFromFormat("Y-m-d", $request->fecha_fin)->endOfDay())
                    ],
                ]
            ],
        ]);
    

    this question helped me

    thank you all for replying


  2. You need to specify the hour, minute and second

    $tracks =  TrackFishing::where('boat_id', '3')
        ->whereBetween(
             'tracking.timestamp', array(
                 Carbon::createFromDate(2022, 7, 5)->startOfDay(),
                 Carbon::createFromDate(2022, 7, 6)->endOfDay()
             ))
        ->first();
    
    Login or Signup to reply.
  3. try with this one;

        'tracking.timestamp', [
            Carbon::createFromDate(2022, 7, 5),
            Carbon::createFromDate(2022, 7, 6)
        ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search