在Mongo中搜索对象数组,查找数组中包含的任何值

时间:2015-02-24 15:50:32

标签: arrays mongodb

我试图搜索Mongo以查看我的数组中是否有任何字符串用于我的" images.src.large":

要搜索的字符串:

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

我的Mongo系列中的内容示例:

{
    "_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"的值来查看图像是否被使用。

1 个答案:

答案 0 :(得分:2)

使用文档中this page中描述的$in运算符。您可以在字段中查询多个值中的任何一个。因此,示例文档中的以下查询将得到以下结果。

> 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