title: Mongoose - how to determine whether a nested object already exist in a query?
body: <p>My database stores a <code>User</code>, which has an array of <code>watchMovies[]</code> containing a nested object <code>movie</code>.</p>
  
  <p>When receiving an update/put API request, containing a <code>watchedMovie</code> object, <strong>I want to check whether the <code>movie</code> with <code>id = zzz</code> already exist in the <code>watchedMovie[]</code> array</strong>.</p>
  
  <p>The problem is, this query doesn't do it. What am I doing wrong?</p>
  
  <pre><code>User.find(
    { username: req.params.username, 'watchedMovies.movie.id': movie.id },
    function (err, count) { 
      if (!err) exist = true;
    }
  );  
  </code></pre>
  
  <h2>Full code below:</h2>
  
  <p><strong>Schema</strong></p>
  
  <pre><code>var userSchema = new Schema({
      username: { type: String, require: true },
      firstName: String,
      lastName: String,
      ...
      watchedMovies : [{ 
          movie: { type: Schema.Types.ObjectId, ref: 'Movie' },
          time: Date,
          watchedDuration: Number,
      }],
  })
  </code></pre>
  
  <p><strong>JSON request body</strong></p>
  
  <pre><code>{
      "id": "10-things-i-hate-about-you", //movie id
      "time": "2012-05-29T17:57:30.169Z",
      "watchedDuration": "88"
  }
  </code></pre>
  
  <p><strong>Mongoose query</strong></p>
  
  <pre><code>router.put('/:username/watchedMovies', function (req, res, next) {
  
    //find movie
    Movie.findOne({ id: req.body.id }, function (err, movie) {
  
      if (err) res.send(err);
  
      //find if user already has the movie in the watchedMovies list
      var exist = false;
      User.find(
        { username: req.params.username, 'watchedMovies.movie.id': movie.id },
        function (err, count) { 
          if (!err) exist = true;
        }
      );   
  
      //if movie is already in watchedList, update User with it
      if (exist) {
        //set existing watchedMovie object
        User.update(...$set...)
      } else { // push new
        User.findOneAndUpdate(...$push...) 
      }
  
    });
  
  });
  </code></pre>