如何解决如何加入不使用objectId的猫鼬模式
我不知道该怎么做,希望有人能帮助我。
我有一个userSchema,其中有一个棋盘游戏列表,其中包含每个棋盘游戏的某些信息。我想将它与boardgameSchema一起显示棋盘游戏信息(即标题)。但是,由于该信息来自boardgamegeek,因此我必须使用其id代替objectId作为参考。
我试图将其虚拟化然后填充,但似乎无法正常工作。请让我知道如何解决此问题。
const userSchema = new mongoose.Schema(
{
name: {
type: String,trim: true,required: true,},boardgames: [
{
bggId: { type: String,unique: true },wantToPlay: Boolean,numOfPlay: Number
},],});
userSchema.virtual("boardgameColl",{
ref: "Boardgame",localField: "boardgames.bggId",foreignField: "bggId",});
BoardgameSchema
const boardgameSchema = new mongoose.Schema({
bggId:{
type: String,unique: true,index: true
},title: {
type: String,required: true
}
});
User.findById(id)
.populate("boardgameColl")
.exec((err,user) => {
if (err || !user) {
return res.status(400).json({
error: "User not found",});
}
req.profile = user; // adds profile object in req with user info
next();
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。