2014-01-09 61 views
0

這是我第一次在這裏發佈在stackoverflow。我希望有人可以幫我解決與mongoDB PHP有關的問題。我更像是一個PHP mySql傢伙,但我們突然需要切換到mongoDB。下面是我可以給出的最多解釋中的問題情況。PHP mongoDB獨特選擇

想象一下,在mysql中,我有一個名爲「照片」的表格,其中包含3個字段(img_id,owner_id和file_path)。而下面的記錄:

記錄#1: 1 --- 1 --- image01.jpg

記錄#2: 2 --- 2 --- image02.jpg

記錄#3: 3 --- 2 --- image03.jpg

然後我有另一個表名爲 「用戶」 與2場(USER_ID,全名)下面的記錄:

記錄#1: 1 --- JHON Doe的

記錄#2: 2 ---馬克凱恩

我所需要的是,以顯示結果類似於下面:

1.) John Doe has image and the image id is 。 - image01.jpg

2.)馬克凱恩具有圖像和圖像ID是和。 - image01.jpg,image02.jpg

請讓我知道這應該如何在mongoDB PHP中完成。提前致謝。

回答

0
$m = new Mongo(); 
$collection = $m->users; 
$collection1 = $m->photos; 
$cursor = $collection->find(); 
$cursor1 = $collection1->find(); 

$count = 0; 
$result = array(); 
$path = array(); 
$str = ""; 
$str1 = ""; 
foreach($cursor as $obj){ 
    foreach($cursor1 as $obj1){ 
     if($obj['user_id'] == $obj1['owner_id']){ 
      $result[$count] = $obj1['img_id']; 
      $path[$count] = $obj1['file_path'];   
      $count++; 
     } 
    } 
    if($count > 1){ 
     for($i = 0; $i < $count; $i++){ 
      $str .= $result[$count]." and "; 
      $str1 .= $path[$count]." , "; 
     }  
    } 
    else{ 
     $str = $result[0]; 
     $str1 = $path[0]; 
    } 
    print "$obj['fullname'] has $count image and the image id is $str - $str1 ."; 
} 

希望上面的代碼塊給你所期望的結果,代碼沒有經過測試,所以一些編譯或語法錯誤可能是有的,但我希望你能解決這些問題。