使用MongoDB从集合中获取每个类别中的10个对象

时间:2017-07-11 13:24:51

标签: javascript node.js mongodb mongoose

我正在尝试使用mongoose从集合中检索总共50个对象。

但我不想要最近的50个,而是每个类别中的10个对象。

每个对象都有一个字段category,其中包含字符串值。例如,我可以做5个不同的请求,并在完成后将它们组合起来。

所以它会像

Post.find({ category: 'A' }.limit(10).then(posts_a => {
  Post.find({ category: 'B' }.limit(10).then(posts_b => {
    Post.find({ category: 'C' }.limit(10).then(posts_c => {
      Post.find({ category: 'D' }.limit(10).then(posts_d => {
        Post.find({ category: 'E' }.limit(10).then(posts_e => {
          return posts_a.concat(posts_b).concat(posts_c).concat(posts_d).concat(posts_e);
        });
      });
    });
  });
});

但是提出5个请求似乎非常无效。仅用1个请求就可以完成同样的工作吗?

0 个答案:

没有答案