2012-10-17 45 views
0

數組我有一個MongoDB的設置,像這樣:PHP MongoDB的查詢與未知的關鍵

fieldName: { 
    "1": { 
     client: "name-here", 
     subfield: "more-junk-here" 
    }, 
    "2": { 
     client: "another-client-name-here", 
     subfield: "more-data" 
    }, 
    "3": { 
     client: "client-num-3", 
     subfield: "i-like-turkey-sandwiches" 
    } 
} 

如何查詢「我樣火雞三明治」,不知道這是在第3位,而沒有搜索整個文檔?我開始寫這一點,但我完全難倒如何做搜索...

$cursor = $collection->findOne(array('fieldName' => 'i-like-turkey-sandwiches)); 

回答

1

(編輯:我的答案是隻有fieldName的內容是子文檔的數組,而不是一個文件用「1」/2有效/ 3鍵和子文檔作爲值)

如果用

$cursor = $collection->findOne(
       array('fieldName.subfield' => 'i-like-turkey-sandwiches')); 

查詢它會回報你,你有一個字段名場內子字段與此值的所有文檔。