I want to query the following Movie
object based on his tags
array with Mongodb
Movie.last.tags
=> [{"type"=>"Genre", "tags"=>["Comedy"]}, {"type"=>"score", "tags" => ["Excellent"]}]
by using $elemMatch
Movie.where({'tags' => {'$elemMatch' => {'type' => "Genre", "tags" => "Comedy"}}})
I can query all entries with the Comedy
tag, but I need to restrict the search to those that have in their tags
array also {"type"=>"score", "tags" => ["Excellent"]}
You found the solution @Veeram, please copy this answer and edit to your wish. I will accept it and I'll be thankful to you for sharing your expertise with us
Movie.where({'tags' => { '$all' => [
{'$elemMatch' => {'type' => "Genre", "tags" => "Comedy"}},
{'$elemMatch' => {'type' => "score", "tags" => "Excellent"}}
]}
})
Thanks a lot Best Regards Fabrizio
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments