title: Trying to understand how mongoose populate or join works
body: <p>I just want to see how to join different collections. Suppose I have a patient collection and doctor collection. I want to do this because a doctor could have a lot of patients and I'm not sure if I should put all the patients into an array of objects in the model field called <code>patients : []</code> or do what I was now trying to practice with populate method mongoose. </p>
  
  <p>Now I have another question. If I go with the populate(join) method all the doctors patients would be all together. I think it is weird because it sound like personal information will be mixed up with different people. I know that populate associates a patient with a doctor by associating an Id with a <code>ref</code>. Is that a good way to do it?</p>
  
  <p>Anyways I tried playing around with populate and I failed miserably. I'll show you the code below. If you can help me make the joining of the 2 collections like the way I described that would be awesome. It would also be great if you addresses some other concerns.</p>
  
  <p>What I tried to do was associate doctor2 with patient 1.</p>
  
  <p>I get an error : </p>
  
  <pre><code>throw new MongooseError.MissingSchemaError(name);
   MissingSchemaError: Schema hasn't been registered for model "Doctor".
  Use mongoose.model(name, schema)
  </code></pre>
  
  <p>Codes</p>
  
  <pre><code>var express = require("express");
  var app = express();
  var mongoose = require("mongoose");
  
  mongoose.connect("mongodb://localhost/population");
  
  var db = mongoose.connection;
  
  db.on("error", console.error.bind(console, "connection error:"));
  db.once("open", function(){
      console.log("connected")
      var doctorSchema = mongoose.Schema({
          name : String,
          address : String,
          username: String,
          password : String,
          patients : [{type : mongoose.Schema.Types.ObjectId, ref: "Patient"}]
      })
      var patientSchema = mongoose.Schema({
          _doctor : {type: mongoose.Schema.Types.ObjectId, ref : "Doctor"},
          name: String,
          illness : String
      })
      //compiling our schem into a Model. A class where we construct documents
      var Doctor = mongoose.model("doctor", doctorSchema );
      var Patient = mongoose.model("patient", patientSchema);
  
      var doctor1 = new Doctor({name : "doc1", address :"add1", username :"user1", password : "pass1"})
      console.log(doctor1.username);
  
      //creating a patient for doctor2
      var doctor2 = new Doctor({name: "doc2", address : "add2", username : "user2", password : "pass2"});
  
      doctor2.save(function(err){
          var patient1 = new Patient({
              name : "pat1",
              illness: "high",
              _doctor: doctor2._id
          })
  
          patient1.save(function(err){
              console.log("saved")
          })      
      })
  
      Patient.findOne({name : "pat1"})
              .populate("_doctor")
              .exec(function(err, patient){
                  console.log("the creator is %s", patient._doctor.name)
              })
  
      })
  
  
  
  
  
  app.listen(3000, function(){
      console.log("listening on port: " , 3000)
  })
  </code></pre>