Sorting objects names by ID reference?

I’m currently working on a sorting system for my project, it uses the Meteor pagination system alethes:meteor-pages.

For sorting it uses the following format:

Pages = new Meteor.Pagination(ProductCollection, {
    templateName: "products_code",
    perPage: 20,
    sort: {
        code: 1, title:1
    }

Now my problem: Some of the variables saved in my ProductCollection refer to ID’s of other collections. For example, one of the variables is “supplier” and it saves a string of the _id of one of the entries in the Suppliers collection.

Is there a way I could make the sorting play nicely with the _id variables? Or is my only option to add an extra field containing the text string rather than the _id and use that for ordering?

(Hope the way I explained this problem makes sense!)

Thanks!

Not really. Especially what’s the use case for sorting by ID? Why would anyone want to sort documents by ID, which is just a random string?

I don’t want to sort by ID, that’s the problem.

The variable “supplier” is a string _id, and not a string name, so if I sort by supplier: 1, it will be in order of ID, rather than in order of suppliers by name. For example, right now the supplier who starts with “U” is coming up BEFORE the supplier who starts with “I”.

The products “supplier” variable is an _id, because suppliers and their abbreviations can be renamed from time to time. So a reference to the _id will keep the product connected to the correct supplier in the event of rename.

A Quick Example of the data structure:

Product{ 
"title": "myTitle",
"code": 1234,
"weight": 10,
"supplier": xxxxxx // (the _id of the supplier)
}

Supplier {
"name": "mySupplier",
"abbreviation": "MYS"
}

And I have a table of Products, that need to be sorted by the “supplier” field. But that is the _id reference. I need to sort it by Supplier name.

I know I can add an extra field to the Product that contains the name of the supplier in addition to the _id, but I am wondering if there is any way to do it through just the _id.

You won’t make it just through the _id. Especially that on the client, your Supplier document may not be subscribed and this way, the Supplier’s name doesn’t even exist.

If you want to sort by Supplier’s name, then the best way is to denormalize and keep Supplier’s name in your Product.

Alternative way is to map either the cursor or the array in order to inject the name field into documents, but for that you will first need the Supplier document published. You can use publish-composite package to make it more automated. You can also get already mapped data through the method and keep it non-reactive.