Filter Products by more than one category

Viewed 33

I am working with Express and MongoDB. I have items like this:

{
  "_id": {
    "$oid": "63107332e573393f34cb4fc6"
  },
  "title": "Eiffel tower",
  "attractionType": [
    {"$oid": "63035678df60496a551a8138"},
    {"$oid": "63035678df60496a551a813d"},
    {"$oid": "63035678df60496a551a8142"},
    {"$oid": "63035678df60496a551a813e"}],

}

{
  "_id": {
    "$oid": "63107332e585673f54hj444"
  },
  "title": "Taj Mahal",
  "attractionType": [
    {"$oid": "63035678df60496ao6567p90"},
    {"$oid": "63035678567h496a551a66gr"},
    {"$oid": "63035678df60496a551a8138"},
    {"$oid": "63035678df60496a551a813d"}],

}

Now I want to filter them with attractionType, I send attraction types as query for Rest API like this:

http://localhost:8000/app/heritages?attractions=63035678df60496a551a813

It's OK when I send one attractionType, but I don't know how I can handle it for more than one attraction type, like this:

http://localhost:8000/app/heritages?attractions=63035678df6d6a51a8136,6303568df6049a551a8137

This is code for one attraction, but I don't know how should be for more than one filter:


  var { attractions } = req.query;
  var lang = req.lang;
  var conditions = { lang: lang };

  if (attractions && attractions.length > 0) {
    conditions.attractionType = attractions;
  }

  try {
    Heritage.find()
      .where(conditions)
      .populate("provinceId attractionType")
      .sort(sortItem)
      .exec((err, doc) => {
        if (err) {
          return res.status(500).json({ message: "server Error", err });
        }
        return res.status(200).json({ message: "seccess", lists: doc });
      });
  } catch (e) {
    return res.status(500).json({ message: "server Error" });
  }
};

1 Answers

by doing http://localhost:8000/app/heritages?attractions[in]=63035678df60496a551a813 should solve your problem

add [in] infront

Related