$skip and $limit are not behaving as excepted. They seem to skip random results in the list of results

I have an aggregation pipeline that is returning unexpected results. It’s a long pipeline so I started splitting it and seeing what the results from the first half were before applying both pipelines together. like so:

const preliminaryResults = await Entity.aggregate(prePipeline).exec();

const results = await Entity.aggregate([ ...prePipeline, ...postPipeline ]).exec();

each entity has a field called serialNumber and the results after the preliminary pipeline are getting what I would expect, in the order I would expect. They’re listed in order (serialNumber: 1, then serialNumber: 2, etc.). However, when I apply the postPipeline:

const postSortPipeline = [{ $skip: skip }, { $limit: limit }]; // skip = 3 and limit = 3

I get weird results. I get the document where the serialNumber is 1, the document where the serialNumber is 3 and the document where the serialNumber is 6. Any ideas what might be going on with this?

If you want a specific order you must sort.