Custom sort on pagination
Closed this issue · 3 comments
Hi, i need to sort all my collection with a custom sort function like i see here : https://stackoverflow.com/questions/35141284/custom-sort-for-a-mongodb-collection-in-meteor
How can i do this ? I try but there is no effect
Thanks
Hi,
What kind of custom sorting are you trying to use? The transform
option method mentioned in that answer is not suitable for pagination as it is applied only to retrieved documents, which would mean that on server side all the collection documents should be kept in memory to be able to apply the transform function on them, before paginating them and sending only the needed ones to the client.
Hi, i have a collection with for exemple this documents :
{ _id: 1, value: "foo foo", _id: 2, value: "foo bar", _id: 3, value: "bar bar", _id: 4, value: "foo foo", _id: 5, value: "foo bar", _id: 6, value: "bar bar" }
In my collections i have 1 000documents. I want to sort them with a certain order like all "foo foo" first, then all "foo bar" and all other after.
How can i achieve this ?
Thanks :)
Why not add some additional properties, that you can use to sort after?
{ _id: 1, value: "foo foo", sortPrimary: 1}
{ _id: 2, value: "foo bar", sortPrimary: 2}
{ _id: 3, value: "bar bar", sortPrimary: 3}
{ _id: 4, value: "foo foo", sortPrimary: 1}
{ _id: 5, value: "foo bar", sortPrimary: 2}
{ _id: 6, value: "bar foo bar", sortPrimary: 3}
{ _id: 6, value: "foo bar bar", sortPrimary: 3}
So you could sort using {sortPrimary: 1, value: 1}