title: Mongoose: Unique field if it's not null or an empty string
body: <p>So, I have the following schema:</p>
  <pre><code>const Players = new mongoose.Schema({
      discordID: {
      type: String,
      unique: true,
      required: function () {
          return typeof this.discordID !== &quot;string&quot; &amp;&amp; this.discordID !== null
      }
  })
  </code></pre>
  <p>My point is to make <code>discordID</code> unique, but only if it's not <code>null</code> or <code>&quot;&quot;</code>.</p>
  <p>I heard about sparse, but it doesn't seem to work as I want it to. I get duplication errors every time I'm trying to insert 2+ documents with discordID set to <code>null</code> (I tried <code>sparse: true</code> and <code>index: {sparse: true, unique: true}</code>).</p>
  <p>Is there something I can do? I want to keep this validation on the schema level if it's only possible.</p>