I have two MongoDB collections Customer
and User
in 1:1
relationship. I'm trying to query both documents using Mongoose Population and sort them by User.name
.
Nothing below is working. My Mongoose is 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()
I found How to sort a populated document in find request?, but no success for me.
It would be something like below in SQL:
SELECT customer.*, user.name, user.email, user.phone FROM customer
JOIN user ON customer.user_id = user.id
ORDER BY user.name ASC