title: mongoose — check if ObjectId exists in an array
body: <p>Following is an example model:</p>
  
  <pre><code>UserModel == {
      name: String,
      friends: [ObjectId],
  }
  </code></pre>
  
  <p><code>friends</code> corresponds to a list of <code>id</code> of objects of some other model for example <code>AboutModel</code>.</p>
  
  <pre><code>AboutModel == {
      name: String,
  }
  
  User.findOne({name: 'Alpha'}, function(error, user){
      About.find({}, function(error, abouts){ // consider abouts are all unique in this case
          var doStuff = function(index){
              if (!(about.id in user.friends)){
                  user.friends.push(about.id);
                  about.save();
              }
              if (index + 1 &lt; abouts.length){
                  doStuff(index + 1)
              }
          }
          doStuff(0) // recursively...
      })
  })
  </code></pre>
  
  <p>In this case, the condition 'about.id in user.friends` seems to be always false. How? Is this to do with the type of ObjectId or the way it is saved?</p>
  
  <p>Note: <code>ObjectId</code> is short for <code>Schema.ObjectId</code>; I don't know if that's an issue in itself.</p>