2015-11-15 76 views
0

是否可以從MongoDB集合中與指定查詢匹配的對象數組中檢索僅子文檔使用 REST HTTP接口通過http/REST從MongoLab/MongoDB中檢索唯一匹配的子文檔

有許多已經回答類似的問題,但這些都是使用MongoDB的驅動程序,而不是REST/HTTP

任何可能性?

的樣本數據

 

    [ 
     { 
      "name":"Paul", 
      "addresses":[ 
      { 
       "addressline1":"Street no1", 
       "city":"Delhi" 
      }, 
      { 
       "addressline1":"Street no4", 
       "city":"Chennai" 
      } 
      ] 
     }, 
     { 
      "name":"Rose", 
      "addresses":[ 
      { 
       "addressline1":"Street no5", 
       "city":"Delhi" 
      }, 
      { 
       "addressline1":"Street no7", 
       "city":"Chennai" 
      } 
      ] 
     } 
    ] 

從上面的數據,我只需要得到一個地址,它的名字'匹配「保羅」和「城市」匹配'德里如下

 

    { 
      "addressline1":"Street no1", 
      "city":"Delhi" 
    } 

我有以下查詢在子文檔中搜索。但它返回包含此子文檔查詢的子文檔數組中的所有元素。

 

    https://api.mongolab.com/api/1/databases/[mydb]/collections/[mycollection? 
    apiKey=[myAPIKey] 
    &q={"name":"Paul","addresses.city":{"$regex":"^Delhi$","$options":"i"}} 
    &f={"addresses":1} 

但上面的查詢返回

 

    {  
      "addresses":[ 
      { 
       "addressline1":"Street no1", 
       "city":"Delhi" 
      }, 
      { 
       "addressline1":"Street no4", 
       "city":"Chennai" 
      } 
      ] 
    } 

回答

1

試試這個:

https://...&f={"addresses.$":1} 

請注意,我用的$ projection operator在上面的查詢。

+0

它的工作原理!謝謝。 – saif

相關問題