Ashesh Khatri
Ashesh Khatri

Reputation: 162

Get price range based on products using MongoDB?

I have my products being maintained in Mongo collection.

Please suggest a Mongo query which will fetch price range based on the number of products available and its corresponding price. I have the following product collection:

[
  {
    "_id": "563b2574db70394054f35dc5",
    "category": {
      "id": "101010"
    },
    "brand": {
      "name": "Global Desi"
    },
    "is_active": true,
    "parent_sku": "88903689927175",
    "price": 1234,
    "sku": "1321asas",
    "name": "Buddha print kurta",
    "quantity": 1
  },
  {
    "_id": "563b2574db70394054f35dc6",
    "category": {
      "id": "101010"
    },
    "brand": {
      "name": "Global Desi"
    },
    "is_active": true,
    "parent_sku": "88903689927175",
    "price": 5678,
    "sku": "1322asas",
    "name": "Buddha print kurta",
    "quantity": 1
  },
  {
    "_id": "563b2574db70394054f35dc7",
    "category": {
      "id": "101010"
    },
    "brand": {
      "name": "Global Desi"
    },
    "is_active": true,
    "parent_sku": "88903689927175",
    "price": 2345,
    "sku": "1323asas",
    "name": "Buddha print kurta",
    "quantity": 1
  },
  {
    "_id": "563b2574db70394054f35dc8",
    "category": {
      "id": "101010"
    },
    "brand": {
      "name": "Global Desi"
    },
    "is_active": true,
    "parent_sku": "88903689927175",
    "price": 7890,
    "sku": "1324asas",
    "name": "Buddha print kurta",
    "quantity": 1
  }
]

Upvotes: 2

Views: 3916

Answers (1)

SalaryNotFound
SalaryNotFound

Reputation: 224

range 501 - 1000, for example:

   db.collection.find({price: {$gte: 501, $lte: 1000}}).sort({price:1})

Upvotes: 3

Related Questions