Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do an AND query on an array in mongodb?

I have an array with tags which is part of a document, eg ["red", "green", "blue", "white", "black"]

Now I want to find all documents, which have red AND blue.

like image 324
Piotr Zolnierek Avatar asked Dec 10 '22 18:12

Piotr Zolnierek


1 Answers

Use the $all condition to find records that match both "red" and "blue" conditions.

db.my_collection.find({tags: { $all : ["red","blue"]}})

If you want records that match either "red" or "blue" then you can use the $in condition.

db.my_collection.find({tags: { $in : ["red","blue"]}})
like image 80
Hates_ Avatar answered Feb 04 '23 23:02

Hates_