12

是否有可能有一个模式来引用 Mongo 中的另一个模式?

我有以下内容,我希望 Line 模式中的用户成为 UserSchema 中的用户

var UserSchema = new Schema({
    name: {type: String, required: true},
    screen_name: {type: String, required: true, index:{unique:true}},
    email: {type: String, required: true, unique:true},
    created_at: {type: Date, required: true, default: Date}
});


var LineSchema = new Schema({
    user: [UserSchema],
    text: String,
    entered_at: {type: Date, required: true, default: Date}
});


var StorySchema = new Schema ({
    sid: {type: String, unique: true, required: true},
    maxlines: {type: Number, default: 10}, // Max number of lines per user
    title: {type: String, default: 'Select here to set a title'},
    lines: [LineSchema],
    created_at: {type: Date, required: true, default: Date}
});


var Story = db.model('Story', StorySchema);
var User = db.model('User', UserSchema);
4

1 回答 1

25

是的,有可能

var LineSchema = new Schema({
    user: {type: Schema.ObjectId, ref: 'UserSchema'},
    text: String,
    entered_at: {type: Date, required: true, default: Date}
});

还有一句话,你为什么打电话给LineSchema他们UserSchema?你可以打电话给他们LineUser他们毕竟代表了一条线和一个用户:)

于 2013-02-10T11:01:31.463 回答