Home > Blockchain >  How to set query in URL and how to find Objects by an array of Strings in mongoose?
How to set query in URL and how to find Objects by an array of Strings in mongoose?

Time:03-21

If I have this Schema...

maid = {
    services: [{
        type: String,
        enum: ["cleaning", "cooking", "baby sitting"]
    }]
}
  1. 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.

  1. 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.

CodePudding user response:

  1. Use should use http://company/maids?services=cleaning,cooking
  2. 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

  • Related