title: Saving nested objects in mongoose
body: <p>Coming from a PHP/ MySQL background, I'm struggling with best practices in terms of structuring and saving my data.</p>
  
  <p>I'm trying to create a small application where I can add recipes with multiple ingredients. I have a bunch of pre-populated ingredients as seed data, the schema for which looks like:</p>
  
  <pre><code>var IngredientSchema = new Schema({
      name: {
          type: String,
          trim: true,
          required: true,
          index: {
              unique: true
          }
      },
      created: {
          type: Date,
          default: Date.now
      }
  });
  
  var Ingredient = mongoose.model('Ingredient', IngredientSchema);
  </code></pre>
  
  <p>A recipe currently looks like this:</p>
  
  <pre><code>var RecipeSchema = new Schema({
      name: {
        type: String,
        trim: true
      },
      ingredients: [
        {
          type: Schema.Types.ObjectId,
          ref: 'RecipeIngredient'
        }
      ],
      created: {
        type: Date,
        default: Date.now
      }
  });
  
  var Recipe = mongoose.model('Recipe', RecipeSchema);
  </code></pre>
  
  <p>Finally, I have a RecipeIngredientSchema. Now, this is where my MySQL background might be creeping in; the reason I've done it this way is because I want the one to many relationship between Recipes and Ingredients, but I also want to be able to specify a unit:</p>
  
  <pre><code>var RecipeIngredientSchema = new Schema({
      recipe: {
        type: Schema.Types.ObjectId,
        ref: 'Recipe'
      },
      ingredient: {
        type: Schema.Types.ObjectId,
        ref: 'Ingredient'
      },
      unit: {
        type: Schema.Types.ObjectId,
        ref: 'Unit'
      },
      created: {
        type: Date,
        default: Date.now
      }
  });
  
  var RecipeIngredient = mongoose.model('RecipeIngredient', RecipeIngredientSchema);
  </code></pre>
  
  <p>My question comes in two parts:</p>
  
  <ul>
  <li>Am I going about this in a sensible way in terms data modeling or am I way off?</li>
  <li>What would the process of saving a recipe with multiple ingredients actually look like? </li>
  </ul>
  
  <p>I'm currently thinking the following:</p>
  
  <pre><code>exports.create = function(req, res) {
  
    var recipe = new Recipe(req.body);
  
    recipe.save(function(err, recipe) {
      if (err) {
        return res.jsonp(err);
      } else {
  
        // Loop ingredients
        if (req.body.ingredients) {
          for(var prop in req.body.ingredients) {
            var recipeIngredient = new RecipeIngredient({
              recipeId: recipe._id,
              ingredientId: mongoose.Types.ObjectId(req.body.ingredients[prop])
            });
  
            recipeIngredient.save(function(err, recipeIngredient) {
              if (err) {
                return res.jsonp(err);
              } else {
                recipe.recipeIngredients.push(recipeIngredient._id);
                recipe.save(function(err, recipe) {
                  return res.jsonp(recipe);
                });
              }
            });
          };
        }
      }
    });
  }
  </code></pre>
  
  <p>I feel this is convoluted and generally <em>wrong</em>, so would appreciate some guidance!</p>