使用 Mongoose Dynamic Populate 产生意外结果

Using Mongoose Dynamic Populate with unexpected result

目前有幻灯片和不同类型幻灯片的概念。我正在尝试在幻灯片放映 object 上填充 slides 数组,并将动态参考设置为幻灯片类型,以获取包含不同文档的已填充幻灯片数组。

匹配 http://mongoosejs.com/docs/populate.html 标题下的文档 "Dynamic References" 我的代码如下所示。

// Abreviated slideshow model
var slideshowSchema = mongoose.Schema({
    slides: [{
        slideType: String,
        item: {
            type: Schema.Types.ObjectId,
            refPath: 'slides.slideType'
        }
    }]
}]


// controller get route
app.get('/slideshow/:id', function(req, res, next) {
    var id = req.params.id;
    if(id){
        Slideshow
            .findById(id)
            .populate('slides.item')
            .exec(function(err, slideshow) {
                res.writeHead( 200, { "Content-Type": "application/json" } );
                res.end(JSON.stringify(slideshow));
            });
    }
    else{
        return next();
    }
});


// type of slide trying to be populated
var currentWeatherSchema = mongoose.Schema({

  slideType: {
      type: String,
      required: true
  },

  slideshowId: {
      type: Schema.Types.ObjectId,
      required: true
  }

  // other unique props

});

当直接填充硬编码的 slideType 时,幻灯片会按预期填充,当 运行 上面的代码得到响应时,幻灯片数组填充了正确数量的 objects,但它们看起来像这样而不是预期的文档:

"slides": [
    {
        "_bsontype": "ObjectID",
        "id": {
            "type": "Buffer",
            "data": [
                89,
                126,
                152,
                150,
                243,
                157,
                179,
                147,
                165,
                23,
                247,
                56
            ]
        }
    }
]

我已经无数次阅读这些示例,但无法弄清楚我哪里出错了。有没有人有这样做的经验,或者看看我可能遗漏了什么?

看起来您的填充是 returning 存储的 ObjectId,因为未找到 collection 引用(如果找到并且 object 不匹配,它会return 空)。因此,collection 文档中的 "slideType" 值没有有效的 collection 引用。

  • 为每个模式创建一个模型。
  • 使用创建模型的名称填充 slideType。小心大写。
  • 确保 ObjectId 与正确引用相同。

您可以使用聚合和 (unwind/lookup) 执行此操作:

进程:

在每张幻灯片中展开项目数组

解析数组中的每一项

将解决项目分组到每张幻灯片的一个字段中

类似于:

 db.slideshowSchema.aggregate([
    {
        $match: {_id: id} // find condition
    },
    {
       $unwind: "$slides" // explode slide array of each document
    },
    {
        $lookup: { // resolve dependencies, take object in mycrosscollections where  mycrosscollections._id == $slides.item
            from:  "mycrosscollections",
            "localField": "$slides.item",
            "foreignField": "_id",
            "as": "resolveSlides"
        }
    },
    {
        $group : { // push back resolve items in slides fields, and regroup by document id
            _id: "$_id",
            slides: {$push: "$resolveSlides"}
        }
     }
 ])