'How to set query in URL and how to find Objects by an array of Strings in mongoose?
If I have this Schema...
maid = {
services: [{
type: String,
enum: ["cleaning", "cooking", "baby sitting"]
}]
}
- How would I write query in URL to find all maids having services "cleaning" and "cooking" only.
Example: "http://company/maids?services=cleaning,cooking" or in some other way.
- How to find all maids from database with required services?
Example:
Maid.find({
services: {
$all:req.query.services
}
})
or
Maid.find(req.query);
Please suggest some good ways to do the above.
Solution 1:[1]
- Use should use
http://company/maids?services=cleaning,cooking - Use could use this $all in mongodb like
Maid.find({services:{$all: req.query.services.split(',')}})
ps: Use have to validate the query before search
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tuphamdev96 |
