代码之家  ›  专栏  ›  技术社区  ›  s_cicero

使用Mongoose动态填充产生意外结果

  •  1
  • s_cicero  · 技术社区  · 8 年前

    目前有幻灯片的概念和不同类型的幻灯片。我正在尝试填充 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
    
    });
    

    在硬编码的幻灯片类型上直接填充时,幻灯片按预期填充,在运行上述代码时,我确实收到了响应,幻灯片数组填充了正确数量的对象,但它们看起来像这样,而不是预期的文档:

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

    2 回复  |  直到 8 年前
        1
  •  0
  •   Cleriston    8 年前

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

    • 为每个模式创建一个模型。
    • 用创建模型的名称填充幻灯片类型。小心大写字母。
        2
  •  0
  •   Daphoque    8 年前

    您可以使用聚合和(展开/查找)来完成此操作:

    过程:

    在每张幻灯片中分解项目阵列

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

    类似于:

     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"}
            }
         }
     ])
    
    推荐文章