1

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

在此处输入图像描述

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

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 类,因为每个不同的 mashup 都应该能够拥有现有组件的“实例”(即 Items)。

现在,我是 Mongoose 的新手,我相信事情可以做得不同。欢迎在这里提出任何建议。但是,当我尝试查询填充结果的混搭时,我得到的输出是:

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应该填充,但不是。我想这是因为它期望有一段Component时间它会得到一个WebComponent. 我该如何解决?我应该停止尝试继承吗?还有什么其他方法可以为这个模型创建数据库模式?

4

1 回答 1

1

呵呵..改变

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

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

修复问题。不知道为什么。

于 2013-08-13T23:12:00.037 回答