Query in an array field

Hi everyone,

For showing a specific element in an array.
I verify everything in this query but it does not work: db.movieDetails.find({writers:["Sergio Leone"]}).pretty()
Can you verify it for me please ?
Thank you.

SOmeone please has an idea ?
I see when there is a space between string on an element of the list, it does not work.
But when i do that one, it works fine : db.movieDetails.find({'genres':['Action']}, ({'title':1, 'countries':1}) ).pretty()

Please provide an example:

However I think your problem comes from the misunderstanding of arrays.

The query

writers : [ ‚ÄúSergio Leone‚ÄĚ ]

does not do what you think it should.

This query searches for documents that have an array exactly equals to [ ‚ÄúSergio Leone‚ÄĚ ]. I suspect you want documents that have ‚ÄúSergio Leone‚ÄĚ among its writers. This is expressed by the query

writers : { $in : [ ‚ÄúSergio Leone‚ÄĚ ] }

When the array specified in the $in value contains only one element, you might use the short-cut

writers : ‚ÄúSergio Leone‚ÄĚ

A last warning about the syntax

arrayFieldName : [ arrayElements ]

means strict equality. This means array [ a , b ] is not equal to array [ b , a ].


Thank you for your answer @steevej-1495.

For exemple, this query wil give me all the movies that we must find in the genres = Action and Adventure, or other genres : db.movieDetails.find({'genres': {$in :['Action', 'Adventure']} }, ({'genres':1, 'title':1, 'countries':1}) ).pretty()

And this one wil give exactely movies that genres has only Action and Adventure :
db.movieDetails.find({'genres' :['Action', 'Adventure'] }, ({'genres':1, 'title':1, 'countries':1}) ).pretty()

I understand now.