1

I have two collections - "products" and "categories". Each product has "category_id" attribute.

I need to select last 30 products, and these products should be in different categories.

categories

{
    _id: 'books',
    name: 'Books',
},
{
    _id: 'computers',
    name: 'Computers',
}

products

{
    name: 'The Lean Startup',
    category_id: 'books',
    created: ISODate("2013-05-29T10:37:58.071Z")
},
{
    name: 'Start Small, Stay Small',
    category_id: 'books',
    created: ISODate("2013-05-29T10:37:58.071Z")
}
4

1 回答 1

1

You could use the aggregation framework for your task. http://docs.mongodb.org/manual/reference/aggregation/

Your query would involve

  • $sort by created
  • $group by category_id
  • and probably $limit
于 2013-07-26T06:41:25.633 回答