首页 > 解决方案 > mongodb: query to check if item in an array contains a particular string

问题描述

I have a collection called products with the following items

[
  { 
    name: 'Product 1'
    images: ['http://staging.example.com/1', 'http://production.example.com/2'],
    ... 
  },
  { 
    name: 'Product 2'
    images: ['http://production.example.com/3'],
    ... 
  }
]

I want to find documents that contain string staging in the images array.

I tried couple of things like using $contains but mongo throws error saying I cannot use $contains with array.

标签: arraysmongodb

解决方案


我认为您可能需要在这里进行正则表达式搜索。

尝试:

db.collection.find({images: /staging/});

推荐阅读