title: How to check if item exists in MonoDB array?
body: <p>I have a MongoDB model:</p>
  <pre><code>const userSchema = new Schema = ({
  name: { type: String },
  company: [
      companyId: {
          type: String,
        },
        movies: [
        {
            genre: {
                type: String,
                enum: [
                'horror',
                'comedy',
                'action',
                'romance',
                ],
            },
            ratings: { type: String }
          },
        ]
      ],
  })
  </code></pre>
  <p>In my query, I have an endpoint that pushes a genre to the movies array but I want to check if there is an existing genre with the name already, if it exists, I want to show a message that says it already exists, otherwise, push the new items to the movies array</p>
  <pre><code>    const result = await UserProfile.updateOne(
        {
          _id: id,
          'company.companyId{ $eq: req.params.companyId},
      'company.movies.$.genre': {
        $eq: { genre: req.body.genre},
      },
     }
        },
        {
          $push: {
            'company.$.movies': {
              ...model,
            },
          },
        },
        { new: true, runValidators: true }
      ).catch((err) =&gt; handleErrorThrow(err));
  
      if (result.nModified === 0)
        throw new CustomError(409, 'Movie exists already');
  </code></pre>
  <p>And if I want to remove the array based on another endpoint, I tried the same thing it doesn't work</p>
  <pre><code>const result = await UserProfile.updateOne(
      {
          _id: id
          }
      },
      {
          $pull: {
              company: {
              $elemMatch: {
                  companyId: req.params.companyId,
                  &quot;movies.genre&quot;: {
                      $ne: req.body.genre
                  }
              }
          }
      },
      { new: true, runValidators: true }
  )
  .catch((err) =&gt; handleErrorThrow(err));
  
  if (result.nModified === 0)
        throw new CustomError(409, 'Not exist');
  </code></pre>
  <p>It returned <code>Not exist'</code></p>