2015-02-24 73 views
0

我試圖尋找蒙戈,看是否有在我的數組的字符串被我「images.src.large」使用:搜索對象的數組中蒙戈包含在陣列中的任何值

字符串搜索:什麼將是我的蒙戈集合

['/california/los-angeles/pool.jpg','/california/los-angeles/pizza.jpg'] 

樣品:

{ 
    "_id": someNumber, 
    "name: "Name 1", 
    "images": [ 
     { 
      "title": "Title 1", 
      "src": { 
       "small": "/california/los-angeles/pool-small.jpg", 
       "medium": "/california/los-angeles/pool-medium.jpg", 
       "large": "/california/los-angeles/pool.jpg" 
      } 
     }, 
     { 
      "title": "Title 2", 
      "src": { 
       "small": "/california/los-angeles/oven-small.jpg", 
       "medium": "/california/los-angeles/oven-medium.jpg", 
       "large": "/california/los-angeles/oven.jpg" 
      } 
     } 
    ], 
}, 
{ 
    "_id": someOtherNumber, 
    "name: "Name 2", 
    "images": [ 
     { 
      "title": "Landscape", 
      "src": { 
       "small": "/alaska/badger/dog-small.jpg", 
       "medium": "/alaska/badger/dog-medium.jpg", 
       "large": "/alaska/badger/dog.jpg" 
      } 
     }, 
     { 
      "title": "Food", 
      "src": { 
       "small": "/alaska/badger/pizza-small.jpg", 
       "medium": "/alaska/badger/pizza-medium.jpg", 
       "large": "/alaska/badger/pizza.jpg" 
      } 
     } 
    ], 
}, 
{ 
    "_id": aDifferentNumber, 
    "name: "Bridge", 
    "images": [ 
     { 
      "title": "Store", 
      "src": { 
       "small": "/hawaii/kilauea/pool-small.jpg", 
       "medium": "/hawaii/kilauea/pool-medium.jpg", 
       "large": "/hawaii/kilauea/pool.jpg" 
      } 
     }, 
     { 
      "title": "Home", 
      "src": { 
       "small": "/hawaii/kilauea/tree-small.jpg", 
       "medium": "/hawaii/kilauea/tree-medium.jpg", 
       "large": "/hawaii/kilauea/tree.jpg" 
      } 
     } 
    ], 
}, 

基本上,我想要做的是搜索我的收藏,看看是否正在使用的圖像通過搜索清「images.src.large」的值。

回答

2

通過使用操作者$in,在this page的文檔中描述。您可以查詢字段中的任何值。因此,您的示例文檔中的以下查詢將具有以下結果。

> db.images.find({ "images.src.large" : 
     { "$in" : ["/california/los-angeles/pool.jpg", 
       "/california/los-angeles/pizza.jpg"] 
     } 
    }).pretty(); 

{ 
    "_id" : 1, 
    "name" : "Name 1", 
    "images" : [ 
     { 
      "title" : "Title 1", 
      "src" : { 
       "small" : "/california/los-angeles/pool-small.jpg", 
       "medium" : "/california/los-angeles/pool-medium.jpg", 
       "large" : "/california/los-angeles/pool.jpg" 
      } 
     }, 
     { 
      "title" : "Title 2", 
      "src" : { 
       "small" : "/california/los-angeles/oven-small.jpg", 
       "medium" : "/california/los-angeles/oven-medium.jpg", 
       "large" : "/california/los-angeles/oven.jpg" 
      } 
     } 
    ] 
} 

請注意,因爲兩個圖像都用在同一個文檔中,查詢只返回一個文檔。如果包含另一個文檔中存在的值,則該查詢將返回兩個文檔。例如:

> db.images.count({ "images.src.large" : 
     { "$in" : ["/california/los-angeles/pool.jpg", 
       "/alaska/badger/dog.jpg"] 
     } 
    }); 
2