title: Complex Relation in Mongoose
body: <p>I have a restaurant idea, that will have branches and every branch has a menu, and every menu has items with there specified price for this branch! </p>
  
  <p>and my schema is:</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  var eaterySchema = mongoose.Schema({
      name: String,
      type: Number,
      branches: [{type: Schema.Types.ObjectId, ref: 'Branch' }]
  });
  
  var branchSchema = mongoose.Schema({
      id: Number,
      name: String,
      eatery: {type: Schema.Types.ObjectId, ref: 'Eatery'},
      menu: {type: Schema.Types.ObjectId, ref: 'Menu'}
  });
  
  var menuSchema = mongoose.Schema({
      branch: {type: Schema.Types.ObjectId, ref: 'Branch'},
      items: [{type: Schema.Types.ObjectId, ref: 'Item'}]
  });
  
  var itemSchema = mongoose.Schema({
      name: String,
      description: String
  });
  
  var itemMenuSchema = mongoose.Schema({ //Many-To-Many Table
      menu: {type: Schema.Types.ObjectId, ref: 'Menu'},
      item: {type: Schema.Types.ObjectId, ref: 'Item'},
      price: Number
  });
  
  
  var Eatery = mongoose.model('Eatery', eaterySchema);
  var Branch = mongoose.model('Branch', branchSchema);
  var Menu = mongoose.model('Menu', menuSchema);
  var Item = mongoose.model('Item', itemSchema);
  var ItemMenu = mongoose.model('ItemMenu', itemMenuSchema);
  </code></pre>
  
  <p>Does the query below written right to get the menu with the items and the item price for requested branch id ?</p>
  
  <pre><code>Branch
  .findOne({id: '@GivenId'})
  .populate('eatery menu')
  .exec(function(err, doc){
          doc
          .populate('menu.items')
          .exec(function(err, doc){
              ItemMenu
              .find({})
              .where('menu._id').equals(doc.menu._id)
              .exec(function(err, items){
                  doc.push(items);
                  res.send(docs);
              })
          })
      };)
  });
  </code></pre>
  
  <p>and does mongoose provides populate to deal with the last query that I have written in the last callback? and what the better solution to deal with this complex relation?</p>
  
  <p>thanks a lot</p>