Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

node.js - Inheriting Mongoose schemas

I wanted to make a base 'Entity Schema', and other model entities would inherit from it. I did it, kinda, but then strange thing happened.

Those are my schemas:

  • AbstractEntitySchema
  • MessageSchema
  • UserSchema
  • RoomSchema

File: https://github.com/mihaelamj/nodechat/blob/master/models/db/mongo/schemas.js

But in MongoDB, they are all saved in the same document store: 'entity models' not separate ones, like Messages, Users.. Did I get what was supposed to happen, but not what I wanted, separate stores? If so I will just make a basic JSON/object as entity and append the appropriate properties for each entity. Or is there a better way? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection. rather than different documents. It seems that you misunderstand the discriminators of mongoose. Here is one article could help you to catch it correctly.

Guide to mongoose discriminators


Here are some codes sample to meet your requirement, to save the derived schema as separated documents

function AbstractEntitySchema() {   
    //call super        
    Schema.apply(this, arguments);     
    //add                                     
    this.add({                              
        entityName: {type: String, required: false},
        timestamp: {type: Date, default: Date.now},
        index: {type: Number, required: false},
        objectID: {type: String},
        id: {type: String}
    });                                     
};
util.inherits(AbstractEntitySchema, Schema);

//Message Schema
var MessageSchema = new AbstractEntitySchema();
MessageSchema.add({
    text: {type: String, required: true},
    author: {type: String, required: true},
    type: {type: String, required: false}
});

//Room Schema
var RoomSchema = new AbstractEntitySchema();
RoomSchema.add({
    name: {type: String, required: true},
    author: {type: String, required: false},
    messages : [MessageSchema],
});

var Message = mongoose.model('Message', MessageSchema);
var Room = mongoose.model('Room', RoomSchema);

// save data to Message and Room

var aMessage = new Message({
     entityName: 'message',
     text: 'Hello',
     author: 'mmj',
     type: 'article'
    });

 var aRoom = new Room({
     entityName: 'room',
     name: 'Room1',
     author: 'mmj',
     type: 'article'
 });

 aRoom.save(function(err, myRoom) { 
    if (err)
        console.log(err);
    else                                  
        console.log("room is saved"); 
 });

 aMessage.save(function(err) {
    if (err)
        console.log(err);
    else
        console.log('user is saved');
 });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...