2017-07-14 59 views
0

搜索我需要有MongoDB的自Collection

'food_name' => 'fish'

'room_features' => 
    array (
    0 => 'Shower', 
    1 => 'Hairdryer', 
), 

我嘗試下面的代碼搜索集合在MongoDB中。但結果是不正確的。我認爲不允許使用多個$eq(數組中的索引相同)。

array (
    '$and' => 
    array (

    array (
     'food' => 
     array (
     '$elemMatch' => 
     array (
      'food_name' => 
      array (
      '$eq' => 'fish', 
     ), 
     ), 
    ), 
    ), 

    array (
     'room' => 
     array (
     '$elemMatch' => 
     array (
      'room_features' => 
      array (
      '$elemMatch' => 
      array (
       '$eq' => 'Shower' 
       '$eq' => 'Hairdryer' 

      ), 
     ), 
      'roomrate' => 
      array (
      '$eq' => new MongoInt32(2500), 
     ), 
     ), 
    ), 
    ), 


), 
) 

這是我需要搜索的文檔。

array (
    '_id' => new MongoId("59670aca7fafd8342e3c9869"), 
    'subcat_name' => 'Test', 
    'place' => '', 
    'description' => '', 
    'created_date' => '1499970060', 
    'created_by' => 'Admin', 
    'openingtime' => '', 
    'closingtime' => '', 
    'hotel_class_id' => '594245f67fafd87e243c986a', 
    'hotel_type_id' => '594244177fafd884563c9869', 
    'latitude' => '0', 
    'longitude' => '0', 
    'dist_id' => '5911966a7fafd8c83c3c986a', 
    'cat_id' => '58fb230e7fafd883183c986d', 
    'featured' => '0', 
    'visited' => new MongoInt64(5), 
    'subcat_slug' => 'test-trivandrum-1', 
    'image' => NULL, 
    'food' => 
    array (
    0 => 
    array (
     'food_id' => '149992634012642164', 
     'region_id' => '5944ba947fafd883333c9869', 
     'food_name' => 'fish', 
     'type' => 'veg', 
     'rate' => '100', 
    ), 
    1 => 
    array (
     'food_id' => '14999366891994980639', 
     'region_id' => '595c75c17fafd835173c986c', 
     'food_name' => 'curry', 
     'type' => 'veg', 
     'rate' => '1000', 
    ), 
), 
    'room' => 
    array (
    0 => 
    array (
     'room_id' => '14999346791721342880', 
     'roomtype' => 'DELUXE KING ROOM1', 
     'roomrate' => new MongoInt64(2500), 
     'image' => 'beach_icon33.png', 
     'room_features' => 
     array (
     0 => 'Shower', 
     1 => 'Hairdryer', 
    ), 
    ), 
    1 => 
    array (
     'room_id' => '14999346901389554873', 
     'roomtype' => 'DELUXE KING ROOM new', 
     'roomrate' => new MongoInt64(4000), 
     'image' => 'beach_icon34.png', 
     'room_features' => 
     array (
     0 => 'Shower', 
     1 => 'Bathrobe', 
    ), 
    ), 
), 
) 

請給我一種替代方法來搜索數組中的多個項目。 在此先感謝。

+0

對於更廣泛的受衆,PHP Array Notation通常不是一個很好的數據表示。即使是轉儲格式通常也不會給出準確的描述。相反,您應該從「mongo shell」查看時發佈數據。這爲更廣泛的讀者提供了準確的描述。當使用PHP時,最好在問題中包含該標籤,而不是讓人猜測。 –

回答

0

我認爲,如果你想查詢列表,你可以在查詢添加列表, 試試這個

{ 
    "food" : { 
     "$elemMatch": { 
      "food_name" : "fish" 
     } 
    }, 
    "room" : { 
     "$elemMatch": { 
      "room_features" : ["Shower", "Hairdryer"] 
     } 
    }, 
} 

希望這有助於。

相關問題