在Mongoose中填充继承的文档 [英] Populate with inherited document in Mongoose

查看:90
本文介绍了在Mongoose中填充继承的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为以下模型创建数据库架构:

I am trying to create a database schema for the following model:

我不确定用MongoDb表示这种更好的方法是什么,但是由于我正在使用Mongoose并且有一个继承插件,所以我尝试以下方法:

I am not sure what the better way to represent this in a MongoDb would be, but since I am using Mongoose and there is a plugin for inheritance, I am trying the following:

var mongoose = require('mongoose')
    , extend = require('mongoose-schema-extend')
    , Schema = mongoose.Schema
    , ObjectId = mongoose.Schema.Types.ObjectId

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {

    //Mashup and Item are bound (itemSchema is a sub-doc)
    var itemSchema = new Schema({
          pos: { top: Number, left: Number }
        , size: { width: Number, height: Number }
        , component: { type: ObjectId, ref: 'Component' }
    })

    var mashupSchema = new Schema({
          name: String
        , desc: String
        , size: { width: Number, height: Number }
        , items: [itemSchema]
    })

    var componentSchema = new Schema({
          name: String
        , desc: String
    }, { discriminatorKey : '_type' })

    var imageComponentSchema = componentSchema.extend({
          url: String
    })

    var textComponentSchema = componentSchema.extend({
          text: String
    })

    var htmlComponentSchema = componentSchema.extend({
          html: String
    })

    var webComponentSchema = componentSchema.extend({
          page: { type: ObjectId, ref: 'Page' }
        , selector: { type: ObjectId, ref: 'Selector' }
    })

    var pageSchema = new Schema({
          name: String
        , desc: String
        , url: String
        , active: { type: Boolean, default: false }
        , webComponents: [{ type: ObjectId, ref: 'WebComponent' }]
    })

    var selectorSchema = new Schema({
          desc: String
        , url: String
        , cssPath: String
    })

    ///MODELS
    var Mashup = db.model("Mashup", mashupSchema)
    var Component = db.model("Component", componentSchema)
    var ImageComponent = db.model("ImageComponent", imageComponentSchema)
    var TextComponent = db.model("TextComponent", textComponentSchema)
    var HtmlComponent = db.model("HtmlComponent", htmlComponentSchema)
    var WebComponent = db.model("WebComponent", webComponentSchema)
    var Page = db.model("Page", pageSchema)
    var Selector = db.model("Selector", selectorSchema)

    //CREATE
    //a new empty mashup
    //var aMashup = new Mashup({ name: "Test" });
    Mashup.create({ name: "Test" }, function (err, mashup) {
        if (err) return
        console.log("Saved: empty mashup")
        //mashup saved, create a webComponent
        var aWebComponent = new WebComponent({ name: "Map", desc: "A map" })

        //create a page
        var aPage = new Page({ name: "Maps", desc: "Google Maps", url: "http://maps.google.com" })
        aPage.webComponents.push(aWebComponent)
        aWebComponent.page = aPage

        //create a selector
        var aSelector = new Selector({desc: "Just the map", url: "maps.google.com", cssPath: "#map" })
        aWebComponent.selector = aSelector

        //save the component
        aWebComponent.save(function(err) {
            if (err) return
            console.log("Saved: WebComponent")

            aPage.save(function(err) {
                if (err) return
                console.log("Saved: the Page")

                aSelector.save(function(err) {
                    if (err) return
                    console.log("Saved: the Selector")

                    //finally add the item with the new component
                    var item = { pos: { top:6, left:10 }, size: { width:100, height:100}, component: aWebComponent }
                    mashup.items.push(item)
                    mashup.save(function (err) {
                        if (err) return
                        console.log("Saved: mashup with item (WebComponent with Page and Selector)")

                        //POPULATE
                        Mashup
                            .find({})
                            .populate("items.component")
                            .exec(function (err, mashup) {
                                if (err) console.log(err)
                                console.log(mashup);
                            })
                    })
                })
            })
        })
    })
});

这是一个用例场景,用户创建一个Mashup,然后通过创建一个新的WebComponent向其中添加一个新的Item.我需要该Item类,因为每个不同的混搭都应该能够具有现有组件的实例"(即Items).

This is a use case scenario, where a user creates a Mashup and then adds a new Item to it by creating a new WebComponent. I need that Item class because each different mashup should be able to have "instances" (i.e. the Items) of existing Components.

现在,我是Mongoose的新手,并且我确信事情可能会有所不同.欢迎在这里提出任何建议.但是,当我尝试查询填充结果的Mashup时,得到的输出是:

Now, I am new to Mongoose and I am sure things could be done differently. Any suggestion here is welcome. However, when I try to query the Mashups populating the results, the output I get is:

Saved: empty mashup
Saved: WebComponent
Saved: the Page
Saved: the Selector
Saved: mashup with item (WebComponent with Page and Selector)
[ { __v: 1,
    _id: 520a8aae3c1052f723000002,
    name: 'Test',
    items: 
     [ { component: null,
         _id: 520a8aaf3c1052f723000006,
         size: [Object],
         pos: [Object] } ],
    size: {} } ]

component应该被填充,但是不是.我猜这是因为它在得到WebComponent的同时期望Component.我该如何解决?我应该停止尝试继承吗?还有什么其他方法可以为此模型创建数据库模式?

component should be populated but it is not. I guess this is because it expects a Componentwhile it gets a WebComponent. How do I fix this? Should I stop trying with inheritance? What other ways are there to create a DB schema for this model?

推荐答案

Doh ..更改

    var componentSchema = new Schema({
          name: String
        , desc: String
    }, { discriminatorKey : '_type' })

    var componentSchema = new Schema({
          name: String
        , desc: String
    }, { collection : 'components', discriminatorKey : '_type' })

解决此问题.不知道为什么.

Fixes the issue. Not sure why.

这篇关于在Mongoose中填充继承的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆