假設我在貓鼬中運行此查詢:Room.find({}, function(err,docs){}).sort({date:-1}); 這行不通!
3 回答

呼啦一陣風
TA貢獻1802條經驗 獲得超6個贊
今天一直使用Mongoose 3.5(.2)處理此問題,沒有一個答案能幫助我解決此問題。以下代碼片段可以解決問題
Post.find().sort('-posted').find(function (err, posts) {
// user posts array
});
您可以發送所需的任何標準參數find()(例如where子句和return字段),但不發送回調。如果沒有回調,它將返回您鏈接的查詢對象sort()。您需要find()再次調用(帶有或不帶有更多參數-出于效率原因不需要任何參數),這將使您能夠在回調中獲取結果集。

精慕HU
TA貢獻1845條經驗 獲得超8個贊
Post.find().sort({date:-1}, function(err, posts){
});
應該也可以
編輯:
如果出現錯誤,也可以嘗試使用此方法sort() only takes 1 Argument:
Post.find({}, {
'_id': 0, // select keys to return here
}, {sort: '-date'}, function(err, posts) {
// use it here
});
- 3 回答
- 0 關注
- 822 瀏覽
添加回答
舉報
0/150
提交
取消