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?

dfox
  • 79
  • 3
  • This is similar to previous questions like this one: http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – Jason Livesay Sep 05 '16 at 07:48

2 Answers2

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.

Wasiq Muhammad
  • 2,910
  • 3
  • 15
  • 27
1

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

Larry Lu
  • 1,489
  • 3
  • 18
  • 26
  • 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? – dfox Sep 05 '16 at 04:29