title: How to prevent duplicate documents mongodb with mongoose
body: <p>I have a user collection and a portfolio collection on mongodb. The portfolio model references the objectid of the users collection. I'm trying to make it so when a user is logged in, and they try to add document they already have, the router prevents it. Right now all this does is look through all the documents in the portfolio collection but I'm trying to filter it by users first, then search the name to see if it's there.</p>
  
  <pre><code>  portfolio.find({name: req.body.name})
    .count()
    .then(count =&gt; {
      if (count &gt; 0) {
        // There is an existing user with the same username
        res.status(400).json({message: 'Stock already saved!'});
      }else{
       portfolio
       .create({
         user: req.body.user,
         name: req.body.name,
         description: req.body.description,
         symbol: req.body.symbol,
         image: req.body.image,
       })
       .then(portfolioPost =&gt; res.status(201).json(portfolioPost.serialize()))
       .catch(err =&gt; {
         // console.error(err);
         res.status(500).json({ error: 'Something went wrong' });
       });
      }
    })
  </code></pre>
  
  <p>this is the model</p>
  
  <pre><code>const listSchema = mongoose.Schema({
    user: { type: mongoose.Schema.Types.ObjectId, ref: "Users" },
    name: { type: String, required: true },
    description: {type:String, required: true},
    image: {type:String},
    symbol: {type: String, required: true}
  });
  </code></pre>