猫鼬:以没有任何ObjectId的猫鼬填充

时间:2020-04-12 15:55:05

标签: mongodb mongoose mongoose-populate

我有2个显示的模式

DaggerContractApiCompnent

现在,一个端点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属性包含ID数组,例如。 genres

如何在没有任何"genres": [4, 7, 19]的情况下填充genres?我已经尝试过使用普通的ref方法,但是它说

ObjectId

我想将其指向{"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

1 个答案:

答案 0 :(得分:2)

您可以使用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