2014-06-13 63 views
0

我需要查找保存在Mongodb文檔中的數組。Mongodb + PHP =>在多維數組中查找查詢運算符

下面是文檔:

{ 

"hashtag" : "World", 
"topimages" : [ 
     { 
      "cluster" : "france", 
      "id_tw" : "477170636327227393" 
     }, 
     { 
      "cluster" : "france", 
      "id_tw" : "477170636327227396" 
     } 

    ] 

} 

我要搜索如果一個名爲「世界」關鍵「主題標籤」的文件,已經保存在數組「topimages」一個id「id_tw鳴叫「的值爲‘477170636327227393’

我試圖做的是:

$query = array('hashtag' => "World", array('topsimages.$.id_tw' => "477170636327227393")); 
$xpmm->find($query); 

我需要搜索,如果一份名爲‘世界’早已保存在陣列鳴叫‘的topimages’他的鍵「id_tw」。

非常感謝。

解決方案:如果它是一個子文檔,而不是一個數組

$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464"); 

回答

0

topimages.x只會工作。你想$elemMatchMongo DB Documentation

$query = array('hashtag' => "World", 'topsimages' => array("$elemMatch" => array("id_tw" => "477170636327227393"))); 
+0

它不也行:'$查詢=陣列( '#標籤'=> 「法國」,陣列('topimages '=> array('$ elemMatch'=> array(「id_tw」=>「477170636327227393」))));' – Xanarus

+0

用代碼在答案中再試一次。我第一次在topimages周圍添加了一個額外的數組。 – kwolfe

0

使用沒關係:

$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");