从引用ObjectId MongoDB中检索信息



我有这个方案

const friendSchema = new mongoose.Schema(
{
friend_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Users",
},
status: String,
},
{ _id: 0 } 
);

这个模型正在使用

const UserSchema = new mongoose.Schema({
name: {
type: String,
},
password: {
type: String,
},
rate: {
type: Number,
default: 0,
},
friend: [friendSchema],
})

就像用户有朋友一样我想得到每一个用户和他们的朋友(名称率(

friendSchema中的状态是好友请求的状态(已接受-挂起(我使用这个代码

let users = await User.find().select('-password').populate({ path: 'friend.$.friend_id' ,  select: 'name rate' })

当我试着让邮递员使用这条路线时我只得到每个用户和朋友的状态,没有每个朋友的名字率

如何获取每个用户及其好友的信息?

尝试不使用.$,如下所示:

let users = await User.find().select('-password').populate({ path: 'friend.friend_id',  select: 'name rate' })

最新更新