Mongodb filter by categories

I have Product collection. In this collection each document has same keys and different values. Several documents are shown in the example below.

[{
            "productCategory": "Electronics",
            "price": "20",
            "priceCondition": "Fixed",
            "adCategory": "Sale",
            "productCondition": "New",
            "addDescription": "Lorem Ipsum Dolor Sit Amet Consectetur Adipisicing Elit Maxime Ab Nesciunt Dignissimos.",
            "city": "Los Angeles",
            "rating": {
                "oneStar": 1,
                "twoStar": 32,
                "threeStar": 13,
                "fourStar": 44,
                "fiveStar": 1
            },
            "click": 12,
            "views": 3
        },
    {

            "productCategory": "Automobiles",
            "price": "1500",
            "priceCondition": "Negotiable",
            "adCategory": "Rent",
            "productCondition": "New",
            "addDescription": "Lorem Ipsum Dolor Sit Amet Consectetur Adipisicing Elit 
  Maxime Ab Nesciunt Dignissimos.",
            "city": "California",
            "rating": {
                "oneStar": 2,
                "twoStar": 13,
                "threeStar": 10,
                "fourStar": 50,
                "fiveStar": 4
            },
            "click": 22,}

},
{
            "productCategory": "Hospitality",
            "price": "500",
            "priceCondition": "Yearly",
            "adCategory": "Booking",
            "productCondition": "New",
            "addDescription": "Lorem Ipsum Dolor Sit Amet Consectetur Adipisicing Elit Maxime Ab Nesciunt Dignissimos.",
            "city": "Houston",
            "rating": {
                "oneStar": 16,
                "twoStar": 19,
                "threeStar": 28,
                "fourStar": 16,
                "fiveStar": 17
            },
            "click": 102,
            "views": 47
}
]

I would like to search each document with one or more matching search queries to match the document with my search.
In the example below, I will show url query: http://localhost:8080/api/v1/filter?productCondition=New&price=100&productCategory=Hospitality

So far I have tried to solve the filtration using the $or and $and operators but here there is a problem because with the first operator $or when I do a query only the first condition is searched, the rest is omitted, with the second operator $and I have to add all the condition and the problem arises because I will not always have all querys.

I am using the find method to achieve my filtering methods.

 db
        .collection(Index.Add)
        .find({
          $or: [
            { productCategory },
            { price },
            { adCategory },
            { priceCondition },
            { productCondition },
            { city },
          ],
        })
        .limit(pageSize)
        .skip(pageSize * parsePage)
        .toArray();

         db
        .collection(Index.Add)
        .find({
          $and: [
            { productCategory },
            { price },
            { adCategory },
            { priceCondition },
            { productCondition },
            { city },
          ],
        })
        .limit(pageSize)
        .skip(pageSize * parsePage)
        .toArray();

Using $or and $and leads to completely different results.

It is not clear if you want $or or you want $and.

A query is simply a json object and you may use any conditional javascript statement to build your query.

You simply do:

queries = []
if ( typeof productCategory !== undefined ) queries.push( { productCategory } )
if ( typeof price !== undefined ) queries.push( { price } )
/* ... */
if ( typeof city !== undefined ) queries.push( { city } )
c.find( { "$and" : queries } )

Thank you for your response. I think I wrote the question too quickly. I have already fixed the problem with the filtration. You’re right in saying that I should check to see if my query is undefined. Actually, I should start with that. I solved the filtration problem like this. And I used $and operator in my case.

const check = (object: IAdds) => {
      for (let key in {
        productCategory,
        price,
        priceCondition,
        adCategory,
        productCondition,
        city,
      }) {
        if (object[key] !== undefined) return req.query;
      }
    };

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.