How to query an array and require two values?

m001 lesson 2.7 shows us how to query the cast for an exact match and for one value, but how to query for records and require two values?

db.movies.find({ cast: “Jeff Bridges”, cast: “Tim Robbins” }) shows movies that have either actor in the cast.

How do we find movies (like Arlington Road and The Making of Arlington Road) ONLY when they have BOTH Jeff Bridges and Tim Robbins in the cast?

db.movies.find({
    cast: {
        $all: [
            "Jeff Bridges",
            "Tim Robbins"
        ]
    }
}
).pretty()

Hi @gnumskull,

I’m glad your issue got resolved. You can also use the $and operator to achieve the same result.

Something like this :

 db.movies.find({$and:[{cast:"Jeff Bridges"},{cast:"Tim Robbins"}]}) 

Hope it helps!

If you have any other query then please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

1 Like