I’m using GridFS to store images. Each image has a group_id in it’s meta data that basically just defines if they are part of the same original image. So each image, has multiple sizes, the group_id is what groups the same images together.
I want to query this, but grouped by the group_id. So it would return an array of the group_id’s, each with the object’s info (width, id, etc.).
If I just do a find on the records I want, this is what it looks like:
[ { _id: 4f871d4adaf6fa492f000001,
width: '500',
group: '1' },
{ _id: 4f871d4adaf6fa492f000004,
width: '150',
group: '1' },
{ _id: 4f871d4bdaf6fa492f000007,
width: '100',
group: '1' },
{ _id: 4f871d4bdaf6fa492f00000a,
width: '50',
group: '1' },
{ _id: 4f871d51daf6faf42e000001,
width: '500',
group: '2' },
{ _id: 4f871d52daf6faf42e000004,
width: '150',
group: '2' },
{ _id: 4f871d53daf6faf42e000007,
width: '100',
group: '2' },
{ _id: 4f871d53daf6faf42e00000a,
width: '50',
group: '2' } ]
I’ve looked at group and mapreduce, and have tried out various options but nothing seems to work. Any suggestions or examples would be great!
Thank you!
One possible way to do this is with the $group function in the new aggregation framework (Available in version 2.1.0):
http://www.mongodb.org/display/DOCS/Aggregation+Framework+-+%24group
Is this the result that you were hoping to achieve?
A similar result may also be achieved with a Map Reduce Operation:
Hopefully this will help you to achieve your desired results!