Peter
Peter

Reputation: 235

meteor sort find() how?

I would like to make a ToDo list webapp in meteor, where users could define their own goals and sort them, so the higher a goal is, the bigger it's priority becomes.

I have two Collections: meteor:PRIMARY> db.goals.find()

{ "_id" : "ZDCwfD4pWfD7sZ9DB", "gtext" : "Money", "createdAt" : ISODate("2015-12-26T13:15:59.563Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 0 }
{ "_id" : "B9oKRGssNWhjz57dP", "gtext" : "Procrastination", "createdAt" : ISODate("2015-12-26T13:16:06.272Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 3 }
{ "_id" : "famqG5vby9wBo37Ns", "gtext" : "Housework", "createdAt" : ISODate("2016-01-01T15:01:02.256Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 2 }
{ "_id" : "QKWJjTEwZQs5ErrPR", "gtext" : "Getting fit", "createdAt" : ISODate("2016-01-01T16:10:38.302Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 1 }
{ "_id" : "ByNyg9uRBH35XXgdi", "gtext" : "test1", "createdAt" : ISODate("2016-01-06T13:40:15.224Z"), "owner" : "JWdrC3bFgmHXHeDFf", "order" : 0 }

meteor:PRIMARY> db.tasks.find()

{ "_id" : "gNuyxHdyA6HN8JFsd", "text" : "clean up", "createdAt" : ISODate("2015-12-27T11:12:46.186Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "famqG5vby9wBo37Ns", "postponedate" : "", "checked" : false,  "relatedgoal": {    "order": 2,    "_id": "famqG5vby9wBo37Ns" }}
{ "_id" : "6dEkaETWAkhsELP8r", "text" : "Sell something", "createdAt" : ISODate("2015-12-27T11:12:48.669Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "",   "relatedgoal": {    "order": 0,    "_id": "ZDCwfD4pWfD7sZ9DB"} }
{ "_id" : "nCKz9oCkiExz4ekfK", "text" : "Win the lottery", "createdAt" : ISODate("2015-12-27T11:12:51.294Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "",  "relatedgoal": {    "order": 0,    "_id": "ZDCwfD4pWfD7sZ9DB"  } }
{ "_id" : "YvWxaxWM9qvGzkpEK", "text" : "read a magazine", "createdAt" : ISODate("2015-12-27T11:12:41.526Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "B9oKRGssNWhjz57dP", "postponedate" : ""   "relatedgoal": {    "order": 3,    "_id": "B9oKRGssNWhjz57dP"  }}
{ "_id" : "s53vn9tf8C5tyy27q", "text" : "task1", "createdAt" : ISODate("2016-01-06T13:39:36.673Z"), "owner" : "JWdrC3bFgmHXHeDFf", "notes" : "", "priority" : "ByNyg9uRBH35XXgdi", "postponedate" : "" }
{ "_id" : "bEWosRJK4ekAvSuJ6", "text" : "do some situps", "createdAt" : ISODate("2016-01-08T15:47:41.047Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "QKWJjTEwZQs5ErrPR",    "relatedgoal": {    "order": 1,    "_id": "QKWJjTEwZQs5ErrPR"  }}

My ultimate goal is to be able to sort the tasks by the order field of the related goal. I'm using perak/meteor-joins to join the tasks collection with the related goal's oder field. Now my problem is that I cannot get my find() to sort the goals by the oder field.

client.js:

Meteor.subscribe('Tasks');
Meteor.subscribe('Goals');

Template.body.helpers({
    priorityTasks: function() {
        return Tasks.find(
            {postponedate: {$exists: false }},
            {sort: {'relatedgoal.order': -1}}
        )
    }
});

I even tried, this but did not help either:

priorityTasks: function() {
    return Tasks.find(
        {postponedate: {$exists: false }},
        {sort: {relatedgoal.order: 1}}
    )  
}),

Is there a way to sort the tasks by their order field? I was thinking about a transform in the find method, but that's not reactive. I need a reactive solution.

Server.js:

Tasks = new Mongo.Collection("tasks");                            
Goals = new Mongo.Collection("goals");
Tasks.join(Goals, "priority", "relatedgoal", ["order"]);

Meteor.publish('Tasks',function() {
    return Tasks.find({ owner: this.userId}); 
});

Meteor.publish('Goals',function() {
    return Goals.find({ owner: this.userId}); 
});
Sortable.collections = ['goals'];

todo.html

<div class="container-fluid col-md-6">   
    <ul>
    {{#each priorityTasks}}       
        {{> task}}
    {{/each}}
    </ul>
</div> 

<template name="task">
    <li>
        <span class="text">
            <a href="#modal-5" data-toggle="modal">
                <strong>{{text}} - {{relatedgoal.order}}</strong>
            </a>
            <br>
            {{postponedate}}
        </span>
    </li>    
</template>

Upvotes: 1

Views: 190

Answers (1)

imslavko
imslavko

Reputation: 6676

priorityTasks: function() {
  return Tasks.find(
      {
      postponedate: {$exists: false }
      }, {
         sort: {'relatedgoal.order': 1}})  
   },

should work

Upvotes: 0

Related Questions