Mongodb来自收藏:搜索

时间:2017-07-14 06:24:46

标签: php arrays mongodb multidimensional-array mongodb-query

我需要在拥有

的mongodb中搜索集合

'food_name' => 'fish'

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

我尝试了以下代码。但结果不正确。我认为不允许多个$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',
      ),
    ),
  ),
)

请给我一个从数组中搜索多个项目的替代方法。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为如果要查询列表,可以在查询中添加列表, 试试这个

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

希望得到这个帮助。