我的应用程序将允许用户提出问题,我需要将用户数据保存在questionSchema上以稍后呈现数据,我是否可以将用户以及标签保存为questionSchema上的子文档以及所有要访问的数据稍后从questionSchema?它会在子文档上创建一个新的_id还是保存我想要的用户_id(如果我在创建新问题时通过它的话)?
questionSchema
const mongoose = require("mongoose");
const tagSchema = require("./tag.schema.js");
const userSchema = require("./user.schema.js");
// Schema variable
const Schema = mongoose.Schema;
const questionSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
user: {
type: userSchema,
required: true
},
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
tags: {
type: [tagSchema],
required: true
},
upVotes: {
type: Number,
default: 0
},
downVotes: {
type: Number,
default: 0
},
date: {
type: Date,
required: true,
default: Date.now
},
});
// Create model
const Question = mongoose.model('Question', questionSchema);
// Export model
module.exports = Question;
userSchema
const mongoose = require("mongoose");
const mongooseUniqueValidator = require("mongoose-unique-validator");
const trophySchema = require("./trophy.schema.js");
// Schema variable
const Schema = mongoose.Schema;
// Users
const userSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
username: {
type: String,
unique: true,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profilePic: {
type: String,
default: "http://i.imgur.com/AItCxSs.jpg"
},
upVotes: {
type: Number,
default: 0
},
downVotes: {
type: Number,
default: 0
},
experience: {
type: Number,
default: 0
}
trophies: [trophySchema]
});
userSchema.plugin(mongooseUniqueValidator);
// Create model
const User = mongoose.model('User', userSchema);
// Export model
module.exports = User;