Sorting by childId in publish-composite (paginated)


Let’s say I have two collections. Parent and Child, and that I want to display Parents in a paginated view.

I’m following Meteor’s recommended pattern of pagination by including some sort parameter to guarantee a repeatable sequence of records. I have a Skip and Limit reactiveVars of 20 each. That way anytime the users clicks next or clicks on a numbered page, the query will adjust accordingly and just publish the 20 parent records (and their children) that I need.

My issue is that I need to sort by a Child record field.

Is there any way for me to add Child.Number to each top-level parent document in the publication itself (So it’s just on the cursor and not actually in the collection)? Re-sorting by Child.Number on the client side only guarantees that each page will be sorted correctly, but not the sequence of pages.

I can add Child.Number to each Parent document. But I’d rather not add a new field if I don’t have to and perform a migration.

Any insights?

So here’s some code of what I’m trying to accomplish

Meteor.publishComposite('parents', function (skipCount, limit) {

    //Any way I can sort by child document field?
    const sort = { sort: childId:1 }
    return {
    find: function () {
      return Parents.find(query, sort);
    children: [
        find: function (parent) {
          //Or is there any way I can "stuff" a child field into the top-level document so that the sort would work
          return Childs.find({ parentId: parent._id });