title: Preventing duplicating entries with Mongoose and Node.Js
body: <p>So I have a student grades object</p>
  
  <pre><code>[
    {
      _id: '5bf43c42a09e1129b8f0cd4c',
      user: '5bc89dec5f6e1103f808671b',
      StudentGrades: [
        {
          _id: '5bf43daf58f0f803d4e9760b',
          classCode: 'ENG1A0',
          gradeLevel: 12,
          credit: 1,
          mark: 67
        }
      ],
      __v: 0
    }
  ];
  </code></pre>
  
  <p>I use the following backend code to make entries into the database </p>
  
  <pre><code>router.put('/:user_id', function(req, res) {
    let id = req.params.user_id;
    const gradeFields = {
      classCode: req.body.classCode,
      gradeLevel: req.body.gradeLevel,
      credit: req.body.credit,
      mark: req.body.mark
    };
  
    if (gradeFields)
      passport.authenticate('jwt', { session: false }),
        UserGrades.findOneAndUpdate(
          { user: id },
          { $push: { StudentGrades: gradeFields } },
          { new: true },
          { unique: true },
          function(err) {
            if (err) {
              res.send(err);
            } else {
              res.send(gradeFields);
            }
          }
        );
  });
  </code></pre>
  
  <p>Everything is working but at this time, a person can have duplicate classes.   </p>
  
  <p>In the express code i tried using {unique: true} and tried setting the classCode mongoose model to unique as well, but it didn't work.  Help would be appreciated</p>