Meteor and mongodb - reorder items


#1

I have the following schema

{
"_id" : “PbSkep5Auv9ZBeEyR”,
“item” : “Footage”,
“permalink” : “footage”,
“itemIndex” : 23,

}
When I paste them in my remote DB it order them by _id, which seems to be a automatic meteor key. I am using robomongo and am trying to reorder the items by ‘itemIndex’ permanently . How do I do this?


#2

You have to do it in your publications and in the helper or in your route for the template.

For example :

Meteor.publish(‘schema’, function(){
return Schema.find({}, {sort: {itemIndex: 1}});
this.ready()
});

Subscribe in the client Meteor.subscribe(‘schema’);

And in the helper or route:

Template.example.helpers({
schema: function () {
return Schema.find({}, {
sort: { itemIndex: 1 }
});
}
});

Hope it works.


#3

perfect found it in the publish setttings

Meteor.publish('items', function() {

      return Items.find({}, {sort: {itemIndex: 1}});
    
    });

#4

Make sure you also sort them when you request the data client-side, otherwise with reactive data you can run into a situation where items aren’t sorted properly