0

I need help trying to figure out how the best way to create a query to search keywords array.

I want to be able to search with array ["work", "hi"] comparing to an array in schema

["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?

zs2020
  • 53,766
  • 29
  • 154
  • 219
Kettle1
  • 45
  • 1
  • 4

2 Answers2

0
this.resumes.find({$or: [{keywords: term1}, {keywords: term2}]})
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
0

I think using the $in operator would make this easier:

this.resumes.find({ keywords: { $in: [ "work", "hi" ] } })

Basically, an $or on a single array field. From the docs:

$in selects the documents where the field value equals any value in the specified array

kmfk
  • 3,821
  • 2
  • 22
  • 32