【发布时间】:2019-06-17 20:11:24
【问题描述】:
我有两个架构钱包和用户
用户架构:-
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)
})
但是他们两个都没有工作,我搜索了很多,但我没有得到任何适当的解决方案,有没有其他方法可以做到这一点,因为我知道我对此感到沮丧。
提前致谢
【问题讨论】: