0
votes

I am currently encountering such a problem:

I have an object A which has an array property Bs. I would like to show Bs in the UI, but because there are too much data, I have to paginate somehow. The problem is, I did some research, but couldn't find a solution that can first unwind, then paginate.

A:

{
    id : 1,
    Bs : [{
        id: 29
    },{
        id : 3
    }]
}

If I use Aggregation, I can unwind the Bs, but can't paginate them afterwards. Pagination is the major problem.

If I use PagingAndSortingRepository, I can paginate, but can't unwind. Even if I unwind manually afterwards, because I paginated before unwind, the result won't be as desired.

If I use Query - Criteria, I can paginate with skip & limit, but still don't know how to unwind before pagination.

THE BIG QUESTION: How can I unwind first, then paginate the unwinded result?

EDIT:

Example data objects in MongoDB:

{
    id: 1,
    ts: 1488208130000,
    logs: [{
        id: 1,
        type: 4
    },{
        id: 2,
        type: 7
    }]
},
{
    id: 2,
    ts: 1488208150000,
    logs: [{
        id: 2,
        type: 4
    }]
}

I want to query logs with ts between startDate & endDate, desc via ts, also via type if specified. But there are too much data objects for example if you select last week ( they are coming 1 per sec nearly ), so I need pagination. I don't know how many logs will be in a data object. So, I can't paginate first, then unwind logs. The number probably won't match. I may query data collection 2. page with size 100, but when I unwind the logs, there may be 47 of them with the desired type.

1

1 Answers

1
votes

You can actually slice the array in mongo using the $slice operator.

E.g.

db.collection.findOne({_id:1}, {B: $slice: [start, end]})

You can do some pretty awesome things with it. Check the documentation for more examples: https://docs.mongodb.com/manual/reference/operator/projection/slice/