Search code examples
node.jsmongodbfindmongooselimit

Mongoose find documents then limit per variable type, not global


Can I execute a .find() documents but limit the returned documents on a specific field, let say that it will only return 5 groups (schema) for each type (schema field) of group?

I tested .limit but it is a global limit rather than a field specific limit.

I know that I can find then postproccess the returned docs but I want a Mongoose specific way to do this, if any.


Solution

  • A little more of search did the work :D

    In resume, it can be uses the .find().distinct() method.

    Here is the complete answer: