【问题标题】:Sorting a Meteor Cursor排序流星光标
【发布时间】:2023-03-15 02:27:01
【问题描述】:

我正在使用带有 Meteor/Mongo 的 Ionic 2。

我正在尝试对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 排序的。

任何帮助表示赞赏。

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    要获得排序结果,您可能需要先调用 fetch()。所以在上面的例子中这是可行的:

    this.localChatCursor.fetch().forEach(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多