title: One to Many Relationship MongoDb Using Mongoose
body: <p>I am creating a relationship between a User and Address. User can have multiple Address. Here is my schema: </p>
  
  <p><strong>userSchema.js</strong> </p>
  
  <pre><code>const mongoose = require('mongoose')
  const Schema = mongoose.Schema
  const Address = require('./addressSchema')
  
  
  let userSchema = mongoose.Schema({
    name :String,
    age : Number,
    cohort :String,
    addresses : [{type : Schema.Types.ObjectId, ref : 'Address'}]
  })
  
  
  let User = mongoose.model('User',userSchema)
  
  module.exports = User
  </code></pre>
  
  <p><strong>addressSchema.js</strong></p>
  
  <pre><code>const mongoose = require('mongoose')
  
  let addressSchema = mongoose.Schema({
    city : String,
    state :String
  })
  
  let Address = mongoose.model('Address',addressSchema)
  module.exports = Address
  </code></pre>
  
  <p><strong>Adding New User and Address (app.js)</strong> </p>
  
  <pre><code>let user = new User({name : 'Mary', age : 36, cohort : 2019})
  let address = new Address({ city : 'Houston', state : 'TX'})
  
  user.addresses.push(address)
  
  
  user.save(function(error,newUser){
    console.log(newUser)
  })
  </code></pre>
  
  <p><strong>Fetching the users and their addresses</strong>
  // fetch all users</p>
  
  <pre><code>User.find(function(error,users){
  
    users.forEach((user) =&gt; {
      console.log('addresses')
      user.addresses.forEach((address) =&gt; {
        console.log(address.city) // prints undefined
      }) 
    })
  
  })
  </code></pre>
  
  <p>Any ideas what I am doing wrong? </p>