chirag_lad
chirag_lad

Reputation: 249

Sub documents vs Mongoose population

I have the following senario:

A user can login to a website. A user can add/delete the poll(a question with two options). Any user can give there opinion on the poll by selecting anyone of the options.

Considering the above scenario I have three models - Users Polls Options . They are as follows, in order of dependency:

Option Schema

var optionSchema = new Schema({
  optionName : {
    type : String,
    required : true,
  },
  optionCount : {
    type : Number,
    default : 0
  }
});

Poll Schema

var pollSchema = new Schema({
  question : {
    type : String,
    required : true
  },
  options : [optionSchema]
});

User Schema: parent schema

var usersSchema = new Schema({
  username : {
    type : String,
    required : true
  },
  email : {
    type : String,
    required : true,
    unique : true
  },
  password : String,
  polls : [pollSchema]
});

How do I implement the above relation between those documents. What exaclty is mongoose population? How is it different from subdocuments ? Should I go for subdocuments or should I use Mongoose population.

Upvotes: 4

Views: 1282

Answers (1)

kkochanski
kkochanski

Reputation: 2287

As MongoDb hasn't got joins as relational databases, so population is a something like hidden join. It just means that when you have that User model and you will populate Poll Model, mongoose will do something like this:

  1. fetch User
  2. fetch related Polls, by ObjectIds which are stored in User document
  3. put fetched Polls documents into User document

And when you will set User as document and Polls as subdocument, it will just mean that you will put whole data in single document. At one side it means that to fetch User Polls, mongoose doesn't need to run two queries(it need to fetch only User document, because Polls data is already there).

But what is better to choose? It just depends of the case.

If your Polls document will refer in another documents (you need access to Polls from documents User, A, B, C - it could be better to populate it, but not for sure. The advantage of populating is fact, that when you will need to change some Polls fields, you don't need to change that data in every document which is referring to that Polls document(as it will be a subdocument) - in that case in document User, A, B, C - you will only update Polls document. As you see it's nice. I told that it's not sure if populating will be better in that case, because I don't know how you need to retrieve your Polls data. If you store you data in wrong way, you will get performance issues or have some problems in easy data fetch.

Subdocuments are the basic way of storing data. It's great when Polls will be only referring to User. There is performance advantage - mongoose need to do one query instead of two as in population and there is no previously reminded update disadvantage, because you store Polls data only in single place, so there is no need to update other documents.

Basically MongoDb was created to mostly use Subdocuments. As the matter of fact, it's just non-relational database. So in most cases I prefer to use subdocuments. I can't answer which way will be better in your case, because I'm not sure how your DB looks like(in a full way) and how you want to retrieve your data.

There is some useful info in official documentation:

Take a look on that.

Edit

As I prefer to fetch data easily, take care about performance and know that data redundancy in MongoDb is something common, I will choose to store this data as subdocuments.

Upvotes: 7

Related Questions