My MongoDB версия 3.2, мангуст версия 4.6.0рода в заселить не работает (Мангуста)
Это мои схемы:
// 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);
Я хочу, чтобы отсортировать на основе временной метки LastMessage в виде по алфавиту заказ. Я попробовал эти три
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))
Кроме того, независимо от того, я использую -1
или 1
, 'desc'
, или «asc'
для timestamp
, она всегда дает мне такие же результаты:
[{
_id: 57c8a682cde8baf5c36eb1fc,
lastMessage: {
_id: 57c8baa29a293eace7f9be15,
timestamp: 2016-09-01T23:32:50.344Z
}
}, {
_id: 57c8a6d0cde8baf5c36eb1fe,
lastMessage: {
_id: 57c8fabb4362b3c25d828774,
timestamp: 2016-09-02T04:06:19.421Z
}
}]
Что может вызвать это? Благодаря
Update1:
Похоже, что ошибка в Мангуста.
Прослушать this issue on GitHub.
UPDATE2: Это сказал не поддерживается. Но я не знаю, почему ... Использует sort
в populate
неправильно для этого случая?
Это может работать для вас http://stackoverflow.com/questions/16352768/how-to-sort-a-populated-document-in-find-request – abdulbarik
@abdulbarik спасибо, я пошел за ними, но все равно не работаю. –
Какую версию mongoose вы используете? – abdulbarik