Issue with array find

db.movies.find({cast: [“Ethan Coen”, “Joel Coen”]}).count() return 0 - what am I doing wrong

You might not selected the correct database “use video” to select video database, then make your query.

Hi Jim,

You are doing an exact match with cast: ["Ethan Coen", "Joel Coen"] . If you want to find documents that contain cast members Ethan Coen And Joel Coen, try using $in operator like:

db.movies.find({ "cast": { $in: ["Ethan Coen", "Joel Coen"] }}).count() 

You can also $elemMatch/$all, here is the link for Array operators: https://docs.mongodb.com/manual/reference/operator/query-array/index.html?searchProperty=current&query=array%20in

Kanika

1 Like

Thank You Dhayal - I am able to get results if I modify to just “Ethan Coen” so I think my db is ok - just not sure why the exact match will not work.

Thank You Kanika - The tutorial mentioned that I could do this, but I could not get it to work - I will use your “in” predicate and play around some more

Hi Jim,

In video they have suggested with the following members for the cast, So I have updated for the exact match.

db.movies.find( {"cast": ["Jeff Bridges", "Tim Robbins"] } ).count() // return 1

The query you search, there are no records like that, that’s why it’s returning 0.

Source: M001, Chapter 2, Reading Documents: Array Fields, Time of video: 1.12