【问题标题】:Using Mongoose Dynamic Populate with unexpected result使用 Mongoose 动态填充结果出乎意料
【发布时间】:2018-01-06 12:18:02
【问题描述】:

目前有幻灯片和不同类型幻灯片的概念。我正在尝试在幻灯片对象上填充 slides 数组,并将动态引用设置为幻灯片类型,以获取具有不同文档的填充幻灯片数组。

匹配 http://mongoosejs.com/docs/populate.html 标题“动态引用”下的文档,我的代码如下所示。

// 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 上进行直接填充时,幻灯片按预期填充,并且在运行上述代码时,我确实得到了响应,并且幻灯片数组填充了正确数量的对象,但它们看起来像这样预期文件:

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

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

【问题讨论】:

    标签: node.js mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    您可以使用聚合和(展开/查找)来做到这一点:

    流程:

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

    解析数组中的每一项

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

    类似:

     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"}
            }
         }
     ])
    

    【讨论】:

      【解决方案2】:

      看起来您的填充正在返回存储的 ObjectId,因为未找到集合引用(如果找到并且对象不匹配,它将返回 null)。因此,您的集合文档中的“slideType”值没有有效的集合引用。

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

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2021-03-25
        • 1970-01-01
        • 2022-01-27
        • 2016-09-03
        • 1970-01-01
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多