【问题标题】:mongoose: populate in mongoose which doesn't have any ObjectId猫鼬:填充没有任何 ObjectId 的猫鼬
【发布时间】:2020-07-25 04:54:49
【问题描述】:

如图所示,我有 2 个架构

const gameSchema = new mongoose.Schema({
    title: String,
    rating: { type: Number, min: 0, max: 100 },
    genres: { type: Array, ref: 'Genres' }
})
const GenreSchema = new mongoose.Schema({
    id: { type: Number },
    name: String,
    description: String
})
mongoose.model('Games', gameSchema)
mongoose.model('Genres', GenreSchema)

现在一个端点 /api/games 返回一组游戏结果,其中 genres 属性包含 id 数组,例如。 "genres": [4, 7, 19]

如何在没有任何ObjectId 的情况下填充genres?我尝试过使用普通的 ref 方法,但它说

{"stringValue":"\"4\"","kind":"ObjectId","value":4,"path":"_id","reason":{},"message":"Cast to ObjectId failed for value \"4\" at path \"_id\" for model \"Genres\"","name":"CastError"}

我想把它指向id而不是_id

【问题讨论】:

    标签: mongodb mongoose mongoose-populate


    【解决方案1】:

    您可以使用Virtuals 的概念。这是怎么回事:

    修改你的架构文件如下:

    //---------------------------------------------------
    const gameSchema = new mongoose.Schema({
      title: String,
      rating: { type: Number, min: 0, max: 100 },
      genres: [Number],//here you have an array of id of type Number as yours, no ref
    });
    const GenreSchema = new mongoose.Schema({
      id: { type: Number },
      name: String,
      description: String,
    });
    
    gameSchema.virtual("games", {
      ref: "Genres",//this is the model to populate
      localField: "id",//the field used to make the populate, it is the field that must match on the aimed  Genres model <- here is the trick you want!!!  
      foreignField: "genres",//the field to populate on Games model
      justOne: false,
    });
    
     gameSchema.set("toObject", { virtuals: true });//if you are planning to use say console.log
     gameSchema.set("toJSON", { virtuals: true });//if you are planning to use say res.json
    
    mongoose.model("Games", gameSchema);
    mongoose.model("Genres", GenreSchema);
    //-------------------------------------------------
    

    在您尝试填充的文件上,将其放在声明部分:

    //-----------------------------------------------------
    const Games = mongoose.model("Games", gameSchema);
    //---------------------------------------------------
    

    最后但并非最不重要的是,您要填充的位置:

    //----------------------------------------------
    Games.find({})
      .populate("games")
      .exec(function (error, games) {
       //with games you can use things like game.field1, it is actually an JSON object! Print out games and see the fieds for your self, select one and call it using the dot notation! 
        console.log(games);
      });
    //---------------------------------------------
    

    我已经针对我所做的一个问题测试了此解决方案,只是根据您的需要进行了修改,请告诉我它是否适用于您的问题;如果没有,我们可以一起找出如何使我的解决方案满足您的需求。

    一些初步参考

    1. Populate a mongoose model with a field that isn't an id

    【讨论】:

    • 非常感谢!直到现在我做错了......我的意思是相反,我在 GenreSchema 上设置了虚拟。这个工作再次感谢!
    • 很高兴为您提供帮助,这就是堆栈溢出的魔力,我喜欢它!
    猜你喜欢
    • 2020-11-19
    • 2019-09-16
    • 2017-03-23
    • 2018-06-01
    • 2015-07-13
    • 2016-10-10
    • 2020-08-15
    • 2021-05-03
    相关资源
    最近更新 更多