如何判断一个值数组是否都存在于mongodb集合中的数组中?

时间:2022-01-20 01:16:06

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

我有一些看起来像这样的代码(都在mongodb解释器中):

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

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.

任何帮助将不胜感激。

1 个解决方案

#1


4  

You want to use $all:

你想使用$ 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} }); 

#1


4  

You want to use $all:

你想使用$ 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} });