Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort in populate does not work well #4481

Closed
hongbo-miao opened this issue Sep 2, 2016 · 3 comments
Closed

sort in populate does not work well #4481

hongbo-miao opened this issue Sep 2, 2016 · 3 comments

Comments

@hongbo-miao
Copy link

hongbo-miao commented Sep 2, 2016

Originally asked on Stack Overflow.

My MongoDB version 3.2, mongoose version is 4.6.0

These are my schemas:

    // chat
    const chatSchema = new mongoose.Schema({
      users: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }],
      lastMessage:  { type: mongoose.Schema.Types.ObjectId, ref: 'Message' }
    });
    export const ChatModel = mongoose.model('Chat', chatSchema);

    // message
    const messageSchema = new mongoose.Schema({
      user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
      chat: { type: mongoose.Schema.Types.ObjectId, ref: 'Chat', required: true },
      text: { type: String, required: true },
      timestamp: { type: Date, default: Date.now }
    });
    export const MessageModel = mongoose.model('Message', messageSchema);

I want to sort based on lastMessage's timestamp in a desc order. I tried these three

    ChatModel
      .find({}, 'lastMessage')
      .populate('lastMessage', 'timestamp', null, { sort: { timestamp: -1 }})
      .exec()
      .then(chats => console.log(chats))

    ChatModel
      .find({}, 'lastMessage')
      .populate({
        path: 'lastMessage',
        select: 'timestamp',
        options: { sort: { timestamp: -1 }}
      })
      .exec()
      .then(chats => console.log(chats))

    ChatModel
      .find({}, 'lastMessage')
      .populate('lastMessage', 'timestamp')
      .sort({ 'lastMessage.timestamp': -1 })
      .exec()
      .then(chats => console.log(chats))

Also, no matter I use -1 or 1, 'desc', or 'asc' for timestamp, it always gives me same results:

  [{
    _id: 57c8a682cde8baf5c36eb1fc,
    lastMessage: {
      _id: 57c8baa29a293eace7f9be15,
      timestamp: 2016-09-01T23:32:50.344Z
    }
  }, {
    _id: 57c8a6d0cde8baf5c36eb1fe,
    lastMessage: {
      _id: 57c8fabb4362b3c25d828774,
      timestamp: 2016-09-02T04:06:19.421Z
    }
  }]
@vkarpov15 vkarpov15 added this to the 4.6.1 milestone Sep 3, 2016
@vkarpov15
Copy link
Collaborator

Not supported, you should store a lastMessageTimestamp in chat schema if you want to sort chats by the last message timestamp. That approach is much more performant because you can use indexes.

@vkarpov15 vkarpov15 removed this from the 4.6.1 milestone Sep 20, 2016
@iamprahladsinghnegi
Copy link

Hi @vkarpov15
Is this feature supported in mongoosev5.9.7?

@vkarpov15
Copy link
Collaborator

@iamprahladsinghnegi no

@Automattic Automattic locked as resolved and limited conversation to collaborators Mar 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants