How to sort a table?

Hello, I have a question:

If I have the following DB:

    users

[
_id: xxxxx1
name: bernard
status: true

friends {
	idFriend1
	idFriend2
	idFriend3
	...
	}
],

[
_id: xxxxx2
name: nile
status: false

friends {
	idFriend4
	idFriend5
	idFriend6
	...
	}
],

...

I have a table for the current user to see all his friends, with an {{#each}} statement.

Then in the table, you can see the “status” of the friend, obtained via a mongodb query.

Is it possible to sort my table by status, as the criteria is known in a “sub-request”??

Thanks

Michael

http://docs.mongodb.org/manual/reference/operator/aggregation/sort/

So basically you’re doing something like this?:

var friends = Users.find({_id: {$in: friendList}}, {sort: {onlineStatus: 1}});

This returns all user objects though. Might want to pluck some fields if you just want the IDs :slight_smile:
The 1 after onlineStatus means ascending. If you want descending, use -1

Thanks greblak.

So in your example, friendList is the result of my current query, right?
Then I put this line afterwards?

friendList is the

friends {
idFriend4
idFriend5
idFriend6
...
}

for #each user

Sorry but I don’t manage :laughing:

For now, to build my HTML table with #Each I loop through this:

Template.manager.helpers({
      friendList: function () {
      var myid = Meteor.user()._id;
      return Meteor.users.find({_id: myid}, {friends: {'profile.friends':1}});
		}
	});

If I understand well, this produces an array, which I should include in your line:

friends = Meteor.users.find({_id: {$in: friendList}}, {sort: {status: 1}});

So all together:

 Template.manager.helpers({
          friendList: function () {
          var myid = Meteor.user()._id;
          var unsortedList = Meteor.users.find({_id: myid}, {'profile.friends':1});

return Meteor.users.find({_id: {$in: unsortedList}}, {sort: {'profile.status': 1}});
    		}
    	});

But this does not work (no output). See what I’m doing wrong? thx

(friends and status are in a .profile collection).

Any clue n how to fix this please? :smiley:

First, have a look at what you’re doing on unsortedList. There are two problems. I assume you’re trying to extract only the profile.friends property for all matching objects?
Instead try doing:

var unsortedList = Meteor.users.find({_id: myid}, {fields: {'profile.friends':1}});

You have to specify “fields” and then list the fields you want. Just like you do with sort. The reason is namespace crashes. Imagine you have a field called field or sort.

You can, however, do this a lot simpler. Since this is the logged in user, just do:

var unsortedList = Meteor.user().profile.friends;

I’m now assuming that this list is on the form [“id1”, “id2”,…]. It needs to be on this form in order to be processed by $in.

I’m now assuming that this list is on the form [“id1”, “id2”,…]. It needs to be on this form in order to be processed by $in.

Actually no, that’s the pb.

var unsortedList = Meteor.user().profile.friends;

returns full objects, with _id on the first line. How do I get only an array with just the id’s?

Not working:

    	var unsortedList = Meteor.users.find({_id: myid}, {fields: {'profile.friends._id':1}});
    
		return Meteor.users.find({_id: {$in: unsortedList}}, {sort: {'profile.status': 1}});

From a security perspective (And in case they chance something), I’d stay away from putting full user objects in the friends-list. However, you can easily get a list of a single field from a list of documents using http://underscorejs.org/

the _pluck function returns an array like I suggested. From their docs:

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]

I¨’ll leave it to you to figure that out :wink:

Also. learn to use the JavaScript debugger in your browser or IDE or “console.log()”. Helps you figure stuff like this out. Because right now I think you’re in the “Something’s wrong, help me figure out what it is”-state instead of “this is wrong, how do I make it right?”-state :slight_smile: