Mongoose/MongoDB查找输入数组中的元素是否在架构属性关键字数组中 [英] Mongoose / MongoDB find if element in input array is in a schema attribute keyword array
问题描述
我需要帮助来弄清楚如何创建查询关键词数组的最佳方法.
I need help trying to figure out how the best way to create a query to search keywords array.
与模式中的数组相比,我希望能够使用数组["work", "hi"]
进行搜索
I want to be able to search with array ["work", "hi"]
comparing to an array in schema
["work", "school"]
,应返回此文档.现在我正在使用
["work", "school"]
and should return this document. Right now I'm using
this.resumes.find({
keywords: {
$all: term.split(' ')
}
}).limit(50)
.select('-keywords').exec(function (err, resumes) {});
,但这仅在两个都在搜索数组中且都在架构数组中时才起作用.那我该怎么做部分比赛呢?
but this only works if both are in the search array are in the schema array. So how can I do a partial match?
推荐答案
我认为使用$in
运算符将使此操作更容易:
I think using the $in
operator would make this easier:
this.resumes.find({ keywords: { $in: [ "work", "hi" ] } })
基本上是单个数组字段上的$or
.从文档中:
Basically, an $or
on a single array field. From the docs:
$ in选择文档,其中字段值等于 指定的数组
$in selects the documents where the field value equals any value in the specified array
这篇关于Mongoose/MongoDB查找输入数组中的元素是否在架构属性关键字数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!