Friday, 15 July 2011

aggregation framework - MongoDB grouping based on intervals -


I want to group my data based on number interval in the measurement. Can I do it with the aggregation framework, or with some mapping-less function?

I want to group with color and the size is bigger than 5 or smaller. I also want to add for example "Medium" to the size of 3 to 5.

I can group with size and color, but then each individual size will have its own object.

I know that it is to check the size of each individual object by db.collection.find (), and then add them according to my specifications, but it will be very slow.

Examples:

  Objects: {Color: "red", size: 2} {color: "red", size: 4} {color: "blue" Size: 2} {Color: "blue", Size: 1} {Color: "Blue Output:  
  {_id: {color:" red ", Size:" small "} Total size (Color: "red", size: "big"} total size: 0} {_id: {color: "blue", "blue" Size: small} total size: 3} {_id: {color: "blue" size: "large"} total size: 7}  

It is easy to use:

  db.collection.aggregat E ([{" $ group ": {" _id ": {" color ":" $ color "," size ": {" $ cond ": [{" $ lt ": [" $ size ", 3]}" "," sh ("$ Size": ["$ size": ["$ size": ["$ size", "medium", "big"]}]}}}, "total_ size": {"$ sum" : "$ Size"}}}])  

Therefore select the value in the group key and calculate based on current value in the document.

< / Html>

No comments:

Post a Comment