title: how to add only unique entries to a subdocument array in a mongoose schema -- $addToSet
body: <p>I'm building an app to teach myself fullstack web development.  In my app, each user has a recipebox which shows recipes they have added. How do I ensure that only unique entries are added to the users recipe box? I have been fumbling around with $addToSet for the last couple of hours (the docs didn't help too much) but it doesn't seem to be working.  My preference is to get this working with what mongoose provides as I already have a really long function that made it work.</p>
  
  <p>In my insert operation here, recipes are assigned unique IDs, so they always get added it seems.  If this is what mongo does, then it seems that I would need to add a "name" property in my schema and check against that, but I'm not sure.  It's unclear to me whether the ID being assigned is helping or hurting.... Here is my route and DB insert operation. any guidance is appreciated.  How do I ensure that only unique entries are added to the recipe box?  (the done property is for a checkbox on the website, ignore that :) ).  I have included 'populate' since eventually I want to return back the actual recipes and not just their IDs.</p>
  
  <pre><code>app.post('/api/recipebox', function(req, res) {
      var recipe = new Recipe({
          recipeUrl: req.body.recipe.source.sourceRecipeUrl,
          image: req.body.recipe.images[0].hostedSmallUrl,
          numServings: req.body.recipe.numberOfServings,
          Ingredients: req.body.recipe.ingredientLines,
          NutritionalInformation: req.body.recipe.nutritionEstimates,
          name: req.body.recipe.name,
          totalTime: req.body.recipe.totalTime
      });
  
      var userId = req.user._id;
      var query = {
          _id: userId
      };
      var recipeId = recipe._id;
  
      recipe.save(function(err, recipe) {
          if(err) {
              throw err;
          }
  
          User.findByIdAndUpdate(userId, {
              $addToSet: {
  
  
                  recipeBox: {
                      _id: recipeId,
                      name: req.body.recipe.name
                  }
              }
          }).populate('recipeBox').exec(function(err, _user) {
              if(err) {
                  throw err;
              }
  
              console.log(_user);
          });
  
      });
  });
  </code></pre>
  
  <p>I checked, and var recipeId does in fact equal  the "recipe" parameter in the first callback, so pretty confused as to why it's not working -- am I missing something about the callback operations?</p>
  
  <p>UPDATE: </p>
  
  <p>I tried this but it also did not work:</p>
  
  <pre><code>recipe.save(function (err, recipe) {
    if (err) {
      throw err;
    }
    console.log("SECOND PARAMETER RECIPE ID", recipe._id);
    User.findByIdAndUpdate(userId, {
      $addToSet: {
        recipeBox: recipeId
      }
    }).populate('recipeBox').exec(function (err, user) {
      console.log("ANOTHER PARAMETER", user);
      if (err) {
        throw err;
      }
      user.save();
      // broadCastStream();
    });
    // broadCastStream();
  });
  </code></pre>
  
  <p>UserSchema:</p>
  
  <pre><code>var userSchema = new Schema({
    email: {
      type: String,
      unique: true
    },
    password: String,
    resetPasswordToken: String,
    resetPasswordExpires: Date,
    picture: ObjectId,
    name: String,
    facebook: {
      id: String,
      token: String,
      pictures: String
    },
  
     recipeBox: [{_id: ObjectId, name: String, done: Boolean}]
  
  
  });
  </code></pre>