Kuan
Kuan

Reputation: 11389

How to get result value range in MongoDB

All:

I am new to MongoDB and I wonder if I search a field, how can I get the value range base on the results:

There mainly two things I want to ask:

For example:

If I search {"product_name": "pname1"}, and I get several results, then I want to know the price range (suppose each document has a field called price) of those products with name "pname1".

For example:

If I search {"price": {$gt: 100}}, and I get several results, then I want to know the price range of those products with price larger than 100

Thanks

Upvotes: 2

Views: 1118

Answers (2)

bagrat
bagrat

Reputation: 7418

The first part

For your first query you can use MongoDB's aggregation framework, and specifically the $first and $last operations. Particularly, you can query your collection to get the price range for all the products in the following way:

db.yourCollection.aggregate([
                               {$sort: 
                                        {
                                           "product_name": 1, 
                                           "price": 1
                                        }
                               },
                               {
                                  $group:
                                           {
                                              _id: "$product_name",
                                              min_price: {$first: "$price"},
                                              max_price: {$last: "$price"},
                                           }
                               }
                           ])

So if you want the information for a particular product, just add a $match stage to your aggregation pipeline:

{
   $match: {"product_name": "<the product of interest>"}
}

The second part

And something similar for the second query.

db.yourCollection.aggregate([
                               {
                                  $match: {
                                             "price": {$gt: 100}
                                          }
                               },
                               {
                                  $group: {
                                             _id: "$price",
                                             products: {$push: "$product_name"} 
                                          }
                               },
                               {
                                  $sort: {_id: 1}
                               }
                               {
                                  $group: {
                                             _id: null,
                                             min_price_products: {$first: "$products"},
                                             mix_price: {$first: "$_id"},
                                             max_price_products: {$last: "$products"},
                                             max_price: {$last: "$_id"}
                                          }
                               }
                           ])

Upvotes: 2

v.coder
v.coder

Reputation: 1932

There is aggregation framework present in mongodb which can provide a functionality where two or more operations can be combined together.

It provides us with the max and min values of a particular field satisfying a particular query using $match and $group operators. It can be used to find the range.

For the given use case the query looks something like:

 db.collection.aggregate([{$match: {"product_name":"pname1"}}, {$group :{_id:null , maxValue:{$max:"$price"}, minValue:{$min:"$price"}}}]);

Here the query is present inside $match, and max and min values are present inside $group functionality of aggregation.

A detailed description can be found in the links below: http://docs.mongodb.org/manual/reference/operator/aggregation/match/ http://docs.mongodb.org/manual/reference/operator/aggregation/group/

Upvotes: 1

Related Questions