开发者

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

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

var needed_skills = ['mongodb','javascript','c++','p开发者_如何学JAVAhp']

db.applicants.save( { name:'joe', skills:['c++','ruby','css'] } );
db.applicants.save( { 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:

db.applicants.find({skills:'mongodb'})

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

Any help would be greatly appreciated.


You want to use $all:

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

or

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜