我有兩個MongoDB集合Customer
和User
在1:1
的關係。我試圖使用Mongoose Population查詢這兩個文檔,並按User.name
對它們進行排序。正確的語法排序貓鼬3.x填充文件
以下沒有任何工作。我的貓鼬是3.8.19。
Customer
.find({})
.populate("user", "name email phone")
.sort({ "name": 1 })
.exec()
Customer
.find({})
.populate("user", "name email phone", null, { sort: { 'name': 1 } })
.exec()
Customer
.find({})
.populate({
path: "user",
select: "name email phone",
options: { sort: { "name": 1 }}
}).
exec()
Customer
.find({})
.populate({
path: "user",
select: "name email phone",
options: { sort: [{ "name": 1 }]}
})
.exec()
我發現How to sort a populated document in find request?,但對我沒有成功。
這將是類似下面的SQL:
SELECT customer.*, user.name, user.email, user.phone FROM customer
JOIN user ON customer.user_id = user.id
ORDER BY user.name ASC
您無法在填充字段上對文檔進行排序。 http://stackoverflow.com/questions/19428471/node-mongoose-3-6-sort-query-with-populated-field – JohnnyHK 2014-12-05 15:10:42