hagopj13 / node-express-boilerplate

A boilerplate for building production-ready RESTful APIs using Node.js, Express, and Mongoose
MIT License
7k stars 2.06k forks source link

Filter by ref property #109

Closed iamhaaamed closed 3 years ago

iamhaaamed commented 3 years ago

I have this query results: { "results": [ { "price": 3000, "name": "Sg", "user": { "role": "admin", "id": "608cbaa42f8c662457960b94" }, "id": "608e9ef6785cb5002008565a" }, { "price": 3000, "name": "Sg", "user": { "role": "admin", "id": "608cbaa42f8c662457960b94" }, "id": "608e9fdd785cb5002008565b" }, { "name": "Aaa", "user": { "role": "admin", "id": "608cbaa42f8c662457960b94" }, "id": "608ea017785cb5002008565c" } ], "page": 1, "limit": 10, "totalPages": 1, "totalResults": 7 } and when I want to filter the results by user id like this: .../v1/products?user=608ebf9a785cb50020085661 it returns all the results... I want to know that is it possible to filter the queries by ref props?

trasherdk commented 3 years ago

I don't use mongodb, so I don't know, but.. I suggest you look at src/routes/v1/user.route.js and try that approach.

Other than that, go check out Express and Mongoose documentation.

{
  "results": [
    {
      "price": 3000,
      "name": "Sg",
      "user": {
        "role": "admin",
        "id": "608cbaa42f8c662457960b94"
      },
      "id": "608e9ef6785cb5002008565a"
    },
    {
      "price": 3000,
      "name": "Sg",
      "user": {
        "role": "admin",
        "id": "608cbaa42f8c662457960b94"
      },
      "id": "608e9fdd785cb5002008565b"
    },
    {
      "name": "Aaa",
      "user": {
        "role": "admin",
        "id": "608cbaa42f8c662457960b94"
      },
      "id": "608ea017785cb5002008565c"
    }
  ],
  "page": 1,
  "limit": 10,
  "totalPages": 1,
  "totalResults": 7
}