How to tell if an array of values all present in an array in a mongodb collection?

By : Travis

I have some code that looks like this (all within the mongodb interpreter):

var needed_skills = ['mongodb','javascript','c++','php'] { name:'joe', skills:['c++','ruby','css'] } ); { name:'peter', skills:['mongodb','javascript','c++','php','html'] } );

How do I do a query that finds the applicant with all the required skills? Basically I'm looking for documents that have all the array members present in a given array. I know you can do something like this to find if you want it to find one member:


However that is only matching one, where as I want to match all ...

Any help would be greatly appreciated.

By : Travis


You want to use $all:

db.applicants.find({skills: {$all: ['mongodb','javascript','c  ','php','html']} });


var needed_skills = ['mongodb','javascript','c  ','php']

db.applicants.find({skills: {$all: needed_skills} }); 
By : Kai

This video can help you solving your question :)
By: admin