Populate with inherited document in Mongoose

江枫思渺然 提交于 2020-01-05 04:51:03

问题


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

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);
                            })
                    })
                })
            })
        })
    })
});

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.

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 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?


回答1:


Doh.. changing

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

to

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

Fixes the issue. Not sure why.



来源:https://stackoverflow.com/questions/18218080/populate-with-inherited-document-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!