2017-10-17 103 views
1

我正在使用Cosmos DB,我想編寫一個SQL查詢以匹配數組中的多個值。爲了詳細說明,假設您有以下集合:與數組中的多個值匹配的CosmosDB SQL查詢

[ 
    { 
     "id": "31d4c08b-ee59-4ede-b801-3cacaea38808", 
     "name": "Oliver Queen", 
     "occupations": [ 
      { 
       "job_title": "Billionaire", 
       "job_satisfaction": "pretty good" 
      }, 
      { 
       "job_title": "Green Arrow", 
       "job_satisfaction": "meh" 
      } 
     ] 
    }, 
    { 
     "id": "689bdc38-9849-4a11-b856-53f8628b76c9", 
     "name": "Bruce Wayne", 
     "occupations": [ 
      { 
       "job_title": "Billionaire", 
       "job_satisfaction": "pretty good" 
      }, 
      { 
       "job_title": "Batman", 
       "job_satisfaction": "I'm Batman" 
      } 
     ] 
    }, 
    { 
     "id": "d1d3609a-0067-47e4-b7ff-afc7ee1a0147", 
     "name": "Clarke Kent", 
     "occupations": [ 
      { 
       "job_title": "Reporter", 
       "job_satisfaction": "average" 
      }, 
      { 
       "job_title": "Superman", 
       "job_satisfaction": "not as good as Batman" 
      } 
     ] 
    } 
] 

我想編寫一個查詢將返回與「億萬富翁」和「蝙蝠俠」的JOB_TITLE職業的所有條目。只是要清楚結果必須有兩個job_titles。所以在上面的集合中,它只應該返回布魯斯·韋恩。

到目前爲止,我曾嘗試:

SELECT c.id, c.name, c.occupations FROM c 
WHERE ARRAY_CONTAINS(c.occupations, {'job_title': 'Billionaire' }) 
AND ARRAY_CONTAINS(c.occupations, {'job_title': 'Batman' }) 

SELECT c.id, c.name, c.occupations FROM c 
WHERE c.occupations.job_title = 'Batman' 
AND c.occupations.job_title = 'Billionaire' 

這兩者返回空的結果。

在此先感謝

回答

3

您需要使用ARRAY_CONTAINS(陣列,search_value,is_partial_match = TRUE),即查詢:

SELECT c.id, c.name, c.occupations 
FROM c 
WHERE ARRAY_CONTAINS(c.occupations, {'job_title': 'Billionaire' }, true) 
AND ARRAY_CONTAINS(c.occupations, {'job_title': 'Batman' }, true) 
+0

那完美。謝謝。 –