'Mongodb paginate with unique data

I have a collection of books, which basically stores the info of books and a ref of their author.

const bookSchema = new Schema<IBook>({
    name: {
        type: String,
    },
    author: {
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true,
    },
});

Now I want to paginate this collection having a huge number of books but I want only or at max two books from the same author on a single page, just to keep the feed more interesting and unique.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source