2015-06-11 64 views
1

查詢時隱瞞關鍵當我執行使用whereIn這樣的查詢:MongoDB的雄辯

$data = User::whereIn('name', $sample)->select('numbers')->get(); 

我得到這樣的結果:

[{"numbers":["100","101"]},{"numbers":["100","101","103"]}, ...] 

我想知道是否有可能隱藏「數字」鍵,請注意,這不是投影,因爲「數字」是搜索信息的一部分。結果應該是這樣的:

["100","101","100","101","103", ...] 

讓我在幾千結果執行操作。

有沒有辦法做到這一點?

回答

0

你可以嘗試使用在這種情況下,raw()方法和使用本機MongoCollection方法,如aggregate()到組的文件,並按下數字鍵的值到一個數組,然後就可以用你的光標遍歷,像這樣(未經測試):

$data = DB::collection('users')->raw(function($collection) 
{ 
    $cursor = $collection->aggregate(array(
     array(
      '$match' => array('name' => $sample) 
     ), 
     array(
      '$unwind' => '$numbers' 
     ), 
     array(
      '$group' => array(
       '_id' => null, 
       'numbers' => array(
        '$push' => '$numbers' 
       ) 
      ) 
     ), 
     array(
      '$project' => array(
       '_id' => 0, 
       'numbers' => 1 
      ) 
     )  
    )); 

    $elems = array(); 
    // Iterate cursor 
    $current = $cursor; 
    do { 
     $elems[] = $current->$numbers; 
    } while (!($current = $cursor->next())); 

    return array_values($elems);  
});