5

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.

0

2 Answers 2

3

I think you might need regex searching here.

Try:

db.collection.find({images: /staging/});
Sign up to request clarification or add additional context in comments.

4 Comments

Ah! could have guessed that :) Thanks!
You're welcome~
Do you may be also know how to update staging to production using update? db.collection.update({images: /staging/}, { $set: { 'images.$[]': ? } })
staging is part of your string value. Update part of string is out of mongo's hand I think
0
db.products.find({images: /staging/i});

I think this will may help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.