我有一个两个模式的钱包和用户
用户架构:-
var schema = new mongoose.Schema({
first_name: {
type: String,
},
wallet_id:{
type: mongoose.Schema.Types.ObjectId,
ref: 'wallet'
},
}
module.exports = mongoose.model('user', schema);
钱包架构:-
var schema = new mongoose.Schema({
amount: {
type: String,
},
}
module.exports = mongoose.model('wallet', schema);
我想找到所有用户并根据他们的可用钱包金额对他们进行排序,我尝试分配但我无法这样做
这是我的代码
//------------ 这是我第一次尝试 ------------
db['user'].find({})
.populate({
path: 'wallet',
options: { sort: {amount:1}}
})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(err)
})
//------------ 这是我第二次尝试 ------------
db['user'].find({})
.populate({
path: 'wallet',
})
.sort({'wallet.amount':1})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(err)
})
但它们都不起作用,我搜索了很多,但我没有得到任何适当的解决方案,有没有其他方法可以做到这一点,因为我知道我对此感到沮丧。
提前致谢