gpt4 book ai didi

node.js - Mongoose 模式引用

转载 作者:IT老高 更新时间:2023-10-28 13:11:22 25 4
gpt4 key购买 nike

在 Mongo 中是否可以有一个 Schema 来引用另一个 Schema?

我有以下内容,我希望 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);

最佳答案

有可能

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

还有一句话,你为什么叫它们 LineSchemaUserSchema ?你可以称它们为 LineUser,毕竟它们代表了一条线路和一个用户 :)

关于node.js - Mongoose 模式引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14796962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com