我是节点js的新手,我正在尝试使用节点js和mongo DB构建一个app测验引擎。我不确定我需要为测验引擎制作模式。所以任何人都可以帮助我。
以下是用户架构的示例...
var userSchema = new Schema({
name: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});
但就像评论所说的那样,你必须更加具体。
据我所知,测验将由用户提供,它将有问题。所以,你可以制作两个实体:
i)用户实体ii)测验/问题实体
用户实体架构:
module.exports = {
attributes = {
name: {
type: String,
required: true
},
password: {
type: String,
required: true
}
password: {
type: String,
required: true
}
}
};
问题实体架构:
module.exports = {
attributes = {
questionLabel: {
type: 'String',
required: true
},
choices: {
type: 'Array',
required: true
}
};
你好,这是我的架构
enter code here var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var img_schema = new Schema({
title:{type:String,require:true},
creator:{type:Schema.Types.ObjectId, ref: "User" },
extension:{type:String,require:true},
foto:{type:String,require:true},
uso:{type:String,require:true}
});
var Imagen = mongoose.model("Imagen",img_schema);
module.exports = Imagen;
This is the example of user schema. you can replace with your requirement.
// User Schema
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true
},
password: {
type: String
},
email: {
type: String
},
name: {
type: String
},
profileimage:{
type: String
}
});
var User = module.exports = mongoose.model('User', UserSchema);
我建议你使用mongoose来定义你的mongoDB集合模式。 Mongoose促进了nodejs和mongoDB之间的许多进程。您可以使用以下命令安装mongoose:
npm i mongoose
然后创建一个这样的架构:
import mongoose from 'mongoose';
const { Schema } = mongoose; //Pulling schema out of mongoose object
const QuizEngineSchema = new Schema({
name: String,
phoneNumber: Number,
// other data that you need to save in your model
},
{timestamps: true},
{id: false});
//Plugging the Schema into the model
const QuizEngine = mongoose.Model('QuizEngine',QuizEngineSchema);
export default QuizEngine;
希望这可以帮助!