【问题标题】:Use Mongo query in Lumen在 Lumen 中使用 Mongo 查询
【发布时间】:2016-07-27 22:44:55
【问题描述】:

我在我的项目中使用 jenssegers/laravel-mongodb 包。我的项目是用 Laravel Lumen 微框架开发的,现在我如何在 lumen 中使用带有 jenssegers/laravel-mongodb 包的 Blow mongo 查询

db.orders.aggregate([
{
    '$match': {
        'status': 'suspend'
    }
},
{
    '$project':{
        'min_diff': {
            '$ceil': {
                '$divide': [
                    {
                        '$subtract': [
                            new Date(),
                            '$created_at'
                        ]
                    },
                    60 * 1000
                ]
            }
        }
    }
},
{
    '$match': {
        '$or': [
            {
                'min_diff': {
                    '$gt': 5,
                    '$lte': 10
                },
                'latitude': {
                    '$gte': '{MIN_LAT}',
                    '$lte': '{MAX_LAT}'
                },
                'longitude': {
                    '$gte': '{MIN_LON}',
                    '$lte': '{MAX_LON}'
                }
            },
            {
                'min_diff': {
                    '$gt': 10,
                    '$lte': 15
                },
                'latitude': {
                    '$gte': '{MIN_LAT}',
                    '$lte': '{MAX_LAT}'
                },
                'longitude': {
                    '$gte': '{MIN_LON}',
                    '$lte': '{MAX_LON}'
                }
            }
        ]
    }
}
])

注意:我的 Lumen 版本是 5.2,MongoDB 版本是 3.2

【问题讨论】:

    标签: mongodb laravel lumen jenssegers-mongodb lumen-5.2


    【解决方案1】:

    我发现这个解决方案有效:

        $time_5_min_ago = Carbon::now()->subMinute(5);
        $time_10_min_ago = Carbon::now()->subMinute(10);
        $time_15_min_ago = Carbon::now()->subMinute(15);
        $time_20_min_ago = Carbon::now()->subMinute(20);
    
        return Order::where(function ($query)  use ($maxLat_try_one,$minLat_try_one,$maxLon_try_one,$minLon_try_one,$time_5_min_ago,$time_10_min_ago) {
            $query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
                ->whereBetween('source_latitude', [$minLat_try_one,$maxLat_try_one])
                ->where('status', '=', 'pending')
                ->where('created_at', '<', $time_5_min_ago)
                ->where('created_at', '>=', $time_10_min_ago);
        })->orWhere(function ($query)  use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago) {
            $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
                ->whereBetween('source_latitude', [$minLat_try_two,$maxLat_try_two])
                ->where('status', '=', 'pending')
                ->where('created_at', '<', $time_10_min_ago)
                ->where('created_at', '>=', $time_15_min_ago);
        })->orWhere(function ($query)  use ($maxLat_try_three,$minLat_try_three,$maxLon_try_three,$minLon_try_three,$time_15_min_ago,$time_20_min_ago) {
            $query->whereBetween('source_longitude', [$minLon_try_three, $maxLon_try_three])
                ->whereBetween('source_latitude', [$minLat_try_three,$maxLat_try_three])
                ->where('status', '=', 'pending')
                ->where('created_at', '<', $time_15_min_ago)
                ->where('created_at', '>=', $time_20_min_ago);
        })->get($fields);
    

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多