如何运行多个选择查询(30)以使用Laravel 5.6从MongoDB获取数据?

时间:2018-06-01 07:13:16

标签: php mongodb laravel laravel-5.6 jenssegers-mongodb

使用的库和MongoDB集合结构:

我正在使用Jensegger Moloquent和Laravel 5.6从MongoDB中获取数据。以下是名为 foursquare_places 的集合的示例结构。

{
    "_id" : ObjectId("5b0004f9987a8e540e1c1a82"),
    "place_id" : "54f84dce498e211ce5c20c9e",
    "title_AR" : "Carnival Cinemas",
    "title_EN" : "Carnival Cinemas",
    "description_AR" : "",
    "description_EN" : "",
    "best_photo" : "https://igx.4sqi.net/img/general/250x250/28064372_firRxizeOM1GSkYGoXn4CmZ71mSSCSNYpFhjxh6kcV4.jpg",
    "city_AR" : "Kolkata",
    "city_EN" : "Kolkata",
    "state_AR" : "West Bengal",
    "state_EN" : "West Bengal",
    "country" : "IN",
    "postal_code_AR" : "700091",
    "postal_code_EN" : "700091",
    "location_AR" : "177-178, IB Block, Broadway Rd, Sector 3, Salt Lake, Kolkata 700091, West Bengal, India",
    "location_EN" : "177-178, IB Block, Broadway Rd, Sector 3, Salt Lake, Kolkata 700091, West Bengal, India",
    "latitude" : 22.573299568163,
    "longitude" : 88.4136933088303,
    "loc" : {
        "type" : "Point",
        "coordinates" : [ 
            88.4136933088303, 
            22.573299568163
        ]
    },
    "website" : "",
    "email" : "",
    "contact_number_AR" : "+913344341212",
    "contact_number_EN" : "+913344341212",
    "ratings_AR" : "7",
    "ratings_EN" : "7",
    "no_of_comments_AR" : 0,
    "no_of_comments_EN" : 0,
    "categories" : "4d4b7104d754a06370d81259,4bf58dd8d48988d17f941735,4d4b7104d754a06370d81259,4bf58dd8d48988d17f941735",
    "category_array" : [ 
        "4d4b7104d754a06370d81259", 
        "4bf58dd8d48988d17f941735", 
        "4d4b7104d754a06370d81259", 
        "4bf58dd8d48988d17f941735"
    ],
    "created_date" : "2018-05-19 16:35:29",
    "modified_date" : "2018-05-19 16:35:29",
    "photo_saved" : 0,
    "tip_saved" : 0,
    "updated_at" : ISODate("2018-05-19T11:05:29.000Z"),
    "created_at" : ISODate("2018-05-19T11:05:29.000Z")
}

代码说明

我正在创建一个web服务api,它将从App端接收位置和类别字符串(逗号分隔的类别ID)。然后我会搜索集合,根据位置坐标和类别ID查找数据。

这是我的Laravel代码: -

    $mongoPlaceQuery = MongoFourSquarePlaces::where('loc', 'near', [
                '$geometry' => [
                    'type' => 'Point',
                    'coordinates' => [
                        (float)$longitude,
                        (float)$latitude
                    ]
                ],
                '$maxDistance' => intval($radius * 1000),
            ]);
    if(!empty($categoryString))
    {
        $categoryQueryArray = array();            
        $fsCatArray = explode(',',$categoryString);

        // Fetch venue list from MongoDB based on category //
        $mongoPlaceQuery = $mongoPlaceQuery->where(function($q) use($fsCatArray){
            foreach($fsCatArray as $key => $fsc)
            {
                $fsct = trim($fsc, ' ');
                if($key == 0)
                {
                    $q->where('category_array', 'all', [$fsct]);
                }
                else
                {
                    $q->orWhere('category_array', 'all', [$fsct]);
                }

            }
        });
    }

查询使用多个“where”语句,使用“OR”。

面临的问题:

目前,查询返回500个数据。我想显示100个数据。如果我使用100个数据的限制,则会出现一些微不足道的情况 - 返回的数据集可能包含来自一个或两个类别的所有100个数据,而不从其他一些类别中获取任何数据。

因此我们需要从30个类别中的每个类别中获取5个数据。一种解决方案是这样的: -

$placerecord = array();

$mongoPlaceQuery = MongoFourSquarePlaces::where('loc', 'near', [
                    '$geometry' => [
                        'type' => 'Point',
                        'coordinates' => [
                            (float)$longitude,
                            (float)$latitude
                        ]
                    ],
                    '$maxDistance' => intval($radius * 1000),
                ]);
if(!empty($categoryString))
{
    $categoryQueryArray = array();            
    $fsCatArray = explode(',',$categoryString);

    foreach($fsCatArray as $key => $fsc)
    {
        $mongoPlaceRecord = $mongoPlaceQuery->where('category_array', 'all', [$fsct])->limit(5)-get();
        foreach($mongoPlaceRecord as $mp)
        {
            // store the record in $placerecord;
        }

    }
}
else
{
    $mongoPlaceRecord = $mongoPlaceQuery->limit(100)-get();
    foreach($mongoPlaceRecord as $mp)
    {
        // store the record in $placerecord;
    }
}

运行循环,然后从每个类别中提取5个数据。这可能需要更长的执行时间。

是否有任何方法可以同时运行30个查询以获取数据?

0 个答案:

没有答案