2015-06-11 63 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);  
});