javascript - Mongodb data structure on posts, comments, save and likes - Stack Overflow

admin2025-04-22  0

I am just learning how to build websites using MEANJS, and I am structuring my data but unsure on the best practice, I am very new to the NoSql concept.

I need to store:

questions
answers
likes
saved_questions

In my app I enable the user to save questions to be viewed later, as well as they can access any answer they posted. And I provide some stats for each question (i.e. number of likes, number of answers, etc)

Should I create one document for "question" and everything inside of it:

{_id: <ObjectId>,
 user_id: <ObjectId>,
 question: 'how can we....',
 answers: [{user_id: <ObjectId>, answer: ''}],
 likes: [{user_id: <ObjectId>}],
 saves: [{user_id: <ObjectId>}]
}

Or should I make multiple documents for each? Or should I use both methods?

I am just learning how to build websites using MEANJS, and I am structuring my data but unsure on the best practice, I am very new to the NoSql concept.

I need to store:

questions
answers
likes
saved_questions

In my app I enable the user to save questions to be viewed later, as well as they can access any answer they posted. And I provide some stats for each question (i.e. number of likes, number of answers, etc)

Should I create one document for "question" and everything inside of it:

{_id: <ObjectId>,
 user_id: <ObjectId>,
 question: 'how can we....',
 answers: [{user_id: <ObjectId>, answer: ''}],
 likes: [{user_id: <ObjectId>}],
 saves: [{user_id: <ObjectId>}]
}

Or should I make multiple documents for each? Or should I use both methods?

Share Improve this question edited Sep 22, 2017 at 18:01 CommunityBot 11 silver badge asked Jan 7, 2015 at 3:18 Moe KatibMoe Katib 1231 silver badge7 bronze badges 3
  • 2 With NoSQL, your structure heavily depends on the questions you are going to ask the database, which in turn are very use case dependent. Please elaborate your use cases a bit more. Are the ones you mentioned your only use cases? Or are they "only" your prime use cases? If you have other, please add them. – Markus W Mahlberg Commented Jan 7, 2015 at 7:22
  • The application is very simple, user posts questions, and then some other users answer them, the users can save, like and report a question. I need to show in the application all the questions as well as the stats related to each question (number of likes, number of answers, etc). And I need to allow the user to be able to save a question for later review, and let the user access any question he/she saved or answered. – Moe Katib Commented Jan 7, 2015 at 14:47
  • I was thinking of creating a like_count, save_count and answer_count in the question document, and increment each when a relevant action takes place (i.e. if a user liked a question the incLike() would be triggered) – Moe Katib Commented Jan 7, 2015 at 14:50
Add a ment  | 

1 Answer 1

Reset to default 6

I would have at least two database models, maybe one for the User and the other for the Question. One of the great things about the MEAN.JS boiler plate is that it already es with a User module plete with sign-up, login/logout functionality. So you have that out of the way as soon as you deploy your new project.

With that already out of the way, I would use the Yo Generator to create a new CRUD module called Question. You can add the files manually, but Yo helps you do it quickly and accurately by automatically placing the files in the correct place, plete with sample code to help you set it up. To learn how to use the Yo Generator I would look at the Yo Generator Section of the MEAN.JS docs.

From your app's root directory, run yo meanjs:crud-module Question. This will create all of the necessary files you need for the database model, as well as a new module on the front & back ends with samples of how to create/read/update/delete the questions. Now, if you log in, you will see the new module in your menu bar.

Then open app/controllers/models/question.server.model.js. This is the file that you can define your new database model. Depending on how plex/relational you want the data to be, you'd want your Mongoose model to look something like this:

var QuestionSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    title: {
        type: String,
        default: '',
        trim: true,
        required: 'Title cannot be blank'
    },
    question: {
        type: String,
        default: '',
        trim: true
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    },
    answers: {
        type: Array,
        default: []
    },
    likes: {
        type: Array,
        default: []
    },
    saves: {
        type: Array,
        default: []
    }
});

Obviously this is very simplified. You may want to create separate mongoose models for the likes, saves, and reports so you can store more data about each (ie: user_id, date, reason for reporting/saving/liking, etc.) To read more about how to make the perfect mongoose model for your needs, I would definitely check out the docs about Mongoose Schemas at mongoosejs..

I hope that helps!

Continued...

To get a list of a given user's actions, I would go ahead and make a new Mongoose Schema for each type of action (ments, likes, saves), and store the details of user's actions there. For instance, in Answers schema you could store the actual ment, who said it, when they said it, what question it was for etc. Then simply query the action tables for a given user ID to retrieve your list of actions.

So..

var QuestionSchema = new Schema({
created: {
    type: Date,
    default: Date.now
},
title: {
    type: String,
    default: '',
    trim: true,
    required: 'Title cannot be blank'
},
question: {
    type: String,
    default: '',
    trim: true
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
}
});

var AnswerSchema = new Schema({
created: {
    type: Date,
    default: Date.now
},
question: {
    type: Schema.ObjectId,
    ref: 'Question'
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
},
answer: {
    type: String,
    default: '',
    trim: true
} 
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745289710a294609.html

最新回复(0)