1

I use to design 'table' like this

teacher
- id
- name

student
- id
- teacher_id
- name

Just assume 1 teacher can have many students, so I put teacher_id to be able to do join.

But in noSql why should I do multiple document? I can put everything under user and use nested object like

   user = {[
    id:1,
    type:teacher
    student:[{
    id:321
    }]
]}

Imagine my app need to retrieve a list of teacher and student in different tab, still with model I can get the data I need, I just do the filter/manipulation on the client side, correct?

1

2 Answers 2

2

if you use nodejs then i preferred you is to use moongose npm on your node.It use schema and model restriction.Your approach is fine in RDBMS but in mongo you avoid to make joins.

Desgin a schema in this way that match according to your requirements and usage of data availabilty and read or write operations

var mongoose = require('mongoose'); var Schema = mongoose.Schema;

var Teachers = new Schema({
        //_id: ObjectId,
        "name": {
            "type": String,
        },
         "students": [{
            name: String
            }]
        })

module.exports = mongoose.model('Teachers', Teachers);

It avoids your join.it manage all the teachers with their respective students.

Sign up to request clarification or add additional context in comments.

Comments

1

You can filter on the server side and send the filtered data to client. It's more efficient.

1 Comment

but is my approach on how to design schema acceptable? because of the nature of nosql is flexible I don't have to be too 'careful' in defining my db schema? right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.