stack-underflow
stack-underflow copied to clipboard
MongoDB files
Thanks for a great project! Is there any way you can share the mongodb collections schemas?
Hi,
These are the 4 schemas i.e., Answer Schema, Comment Schema, Question Schema, User Schema as follows
------------------------Answer Schema-----------------------
const answerSchema = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
body: {
type: String,
required: true,
trim: true,
minlength: 30,
},
comments: [commentSchema],
points: {
type: Number,
default: 0,
},
upvotedBy: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
downvotedBy: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
});
------------------------Comment Schema-----------------------
const commentSchema = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
body: { type: String, required: true, trim: true, minlength: 5 },
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
});
---------------Question Schema---------------------
const commentSchema = require('./comment').schema;
const answerSchema = require('./answer').schema;
const questionSchema = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
title: {
type: String,
required: true,
trim: true,
minlength: 15,
},
body: {
type: String,
required: true,
trim: true,
minlength: 30,
},
tags: [{ type: String, required: true, trim: true }],
comments: [commentSchema],
answers: [answerSchema],
points: {
type: Number,
default: 0,
},
upvotedBy: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
downvotedBy: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
],
views: { type: Number, default: 0 },
hotAlgo: { type: Number, default: Date.now },
acceptedAnswer: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Answer',
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
});
------------------------------User Schema----------------------
const uniqueValidator = require('mongoose-unique-validator');
const userSchema = new mongoose.Schema({
username: {
type: String,
minlength: 3,
maxlength: 20,
required: true,
trim: true,
unique: true,
},
passwordHash: {
type: String,
required: true,
},
role: { type: String, default: 'user' },
questions: [
{
quesId: { type: mongoose.Schema.Types.ObjectId, ref: 'Question' },
rep: { type: Number, default: 0 },
},
],
answers: [
{
ansId: { type: mongoose.Schema.Types.ObjectId, ref: 'Answer' },
rep: { type: Number, default: 0 },
},
],
createdAt: {
type: Date,
default: Date.now,
},
});
userSchema.plugin(uniqueValidator);