title: How do I query an array within a schema in mongoose?
body: <p>Really stuck on querying an array nested within a schema.</p>
  <p>The list schema looks likes this:</p>
  <pre><code>const listSchema = new mongoose.Schema({
    name: String,
    items: [cardSchema]
  });
  </code></pre>
  <p>I want to be able to query a particular list for a particular item, and figured this would work:</p>
  <pre><code>const listId = req.body.listId;
  const itemId = req.body.itemId;
  
  List.updateOne({
      &quot;_id&quot;: listId
  }, {
      $pull: {
          items: {
              &quot;_id&quot;: itemId
          }
      }
  });
  </code></pre>
  <p>As I understand, I'm updating a list by the id of &quot;listId&quot;, and then removing something from it. The thing I'm removing is within the &quot;items&quot; array and has the id &quot;itemId&quot;. However, this doesn't do anything.</p>
  <p>I have a much clumsier, heavy-handed way of getting round this problem, shown here:</p>
  <pre><code>List.findOne({
      _id: listId
  }, function (err, list) {
      if (err) {
          console.log(err)
      } else {
          const listItems = list.items
          listItems.forEach(function (item) {
              if (item._id == itemId) {
                  const index = listItems.findIndex(function (i) {
                      return i.id === itemId
                  })
                  if (index !== -1) listItems.splice(index, 1);
                  list.save();
              }
          })
      }
  </code></pre>
  <p>But I'd much rather something like the former. The latter just seems ridiculous when nesting arrays within arrays within arrays etc so I know this can't be the sensible way.</p>