マイMongoDBのバージョン3.2、マングースのバージョンは4.6.0ソート(マングース)が動作しません
これらは私のスキーマあるあります注文。
[{
_id: 57c8a682cde8baf5c36eb1fc,
lastMessage: {
_id: 57c8baa29a293eace7f9be15,
timestamp: 2016-09-01T23:32:50.344Z
}
}, {
_id: 57c8a6d0cde8baf5c36eb1fe,
lastMessage: {
_id: 57c8fabb4362b3c25d828774,
timestamp: 2016-09-02T04:06:19.421Z
}
}]
は何これを引き起こす可能性があります。私はtimestamp
ため-1
または1
、'desc'
、または「asc'
を使用していない問題は、それはいつも私に同じ結果が得られ、また、これらの3
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))
を試してみましたか?おかげ
UPDATE1:
それはマングースのバグのように思えます。
this issue on GitHubを追跡してください。
アップデート2: それはサポートされていないと述べました。でも、なぜか分かりません... populate
にsort
を使っていますか?最初に動作していない、これはそれがdatetime型だかどうかを確認するために、例えば、テキストのでそれを確認した場合
Chat.find().populate({
path: 'lastMessage'
, select: 'timestamp'
, options: { sort: { timestamp: -1 }}
}).exec(function (err, chats) {
//do something
})
:
それはあなたのために働くことがありhttp://stackoverflow.com/questions/16352768/how-to-sort-a-populated-document-in-find-request – abdulbarik
@abdulbarikありがとう、私はそれらに従ったが、まだ動作しません。 –
あなたはどのバージョンのマングースを使用していますか? – abdulbarik