MongoDB - 查找数组包含查询数组中任何项目的文档
我有架构
person = {
skill = [{
type: String
}]
name = { type : String}
}
我有一个技能数组
skill = ['python', 'css']
我想要所有与技能数组中至少一项技能相匹配的人。
$all
和 $in
仅检索与 skill
数组中的所有技能匹配的人员,但我希望匹配至少一项技能的人员skill
数组。
I have Schema
person = {
skill = [{
type: String
}]
name = { type : String}
}
I have a skill
array
skill = ['python', 'css']
I want all the people that match at least one skill from the skill array.
$all
and $in
retrieve only people that match all the skills in the skill
array but I want the people that match at least one skill from the skill
array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$setIntersection
。$setIntersection
- 将输入技能数组与skill
字段相交,并返回具有公共(相交)值的数组。$ne
- 过滤文档,结果 (1) 不是空数组。示例 Mongo Playground
You can use
$setIntersection
.$setIntersection
- Intersect the input skill array withskill
field and returns a array with common (intersect) value(s).$ne
- Filter the document with result from (1) is not empty array.Sample Mongo Playground
您可以使用
"$in"
来达到您的目的。也许您之前尝试时遇到了其他问题。在 mongoplayground.net 上尝试一下。
只是为了好玩,这里是另一个 mongoplayground.net 示例,该示例使用 < a href="https://github.com/feliixx/mgodatagen" rel="nofollow noreferrer" title="mgodatagen GitHub repo">mgodatagen 配置来生成 收藏。查询是相同的。
You can use
"$in"
for your purpose. Perhaps you had some other issue when you tried it before.Try it on mongoplayground.net.
Just for fun, here's another mongoplayground.net example that uses a mgodatagen configuration to generate the collection. The query is the same.