title: Create entire new document or update existing in mongodb using mongoose (nodejs)
body: <p>Hi all I am trying to create a new document or update it if it exists</p>
  <p>here is the code</p>
  <pre><code> if (!isMemberr) {
          const newSubscription = new membership({
              user_id: req.body.user_id,
              plan_id: req.body.plan_id,
              transaction_id: req.body.transaction_id
          });
          newSubscription.update({ upsert: true, setDefaultsOnInsert: true }).then(() =&gt; {
              res.status(201).json({
                  Status: 201,
                  Message: &quot;Subscription Created&quot;,
                  Data: newSubscription
              })
          })
  
          const filter = { _id: req.body.user_id };
          const update = { member: true };
          let updatedStatus = await User.findOneAndUpdate(filter, update);
          updatedStatus.member;
          plan()
  </code></pre>
  <p>the issue is if a <strong>particular document already exists then it is updating</strong> it but <strong>when I delete the document and call the API it is not giving any error or does not create a new document</strong>.</p>