我正在使用Ionic 2和Meteor / Mongo。
我正在尝试对Cursor
进行排序,但发现它只保留了插入项目的原始顺序。
模型
interface Chat {
_id?: string;
memberIds?: string[];
title?: string;
subTitle?: string;
picture?: string;
lastMessage?: Message;
lastMessageCreatedAt?: Date;
receiverComp?: Tracker.Computation;
lastMessageComp?: Tracker.Computation;
}
TS
private sortLocalChats(): void {
this.localChatCursor = this.localChatCollection.find({}, { sort: { lastMessageCreatedAt: -1 } });
this.localChatCursor.forEach((chat: Chat) => {
console.log(chat.title+' '+chat.lastMessageCreatedAt);
});
console.log('==> loaded sorted local chats: ' + this.localChatCollection.find().count());
输出
Ashton Marais Thu Oct 06 2016 16:50:36 GMT+0800 (CST)
Ashton Marais Wed Oct 12 2016 21:20:18 GMT+0800 (CST)
ghjghj ghjghg Wed Oct 05 2016 23:37:49 GMT+0800 (CST)
Brett Simpson Thu Oct 06 2016 23:52:05 GMT+0800 (CST)
==> loaded sorted local chats: 4
我原本预计会按lastMessageCreatedAt
排序。
任何帮助表示感谢。
答案 0 :(得分:0)
要获得排序结果,您可能需要先调用fetch()。所以在上面的例子中,这有效:
this.localChatCursor.fetch().forEach(...);