title: Mongoose get objects which match an object item from an array of objects
body: <p>I would like to find a single document matching the <strong>courseID</strong> but inside the document only objects from the <strong>materials</strong> array whose <strong>moduleNo</strong> matches the one I give. But the query I currently use seems to return the correct document but also returns all the objects in <strong>materials</strong> array. Any help would be appreciated.</p>
  <p>My schema,</p>
  <pre><code>const materialSchema = new mongoose.Schema({
    courseID: String,
    materials: [
      {
        moduleNo: Number,
        moduleMaterial: String,
      },
    ],
  });
  </code></pre>
  <p>My code/query,</p>
  <pre><code> exports.getMaterials = (req, res) =&gt; {
    const { courseID, moduleNo } = req.query;
    Material.findOne(
      { courseID, &quot;materials.moduleNo&quot;: moduleNo },
      (err, result) =&gt; {
        if (err) {
          console.error(err);
        } else {
          res.json(result);
        }
      }
    );
  };
  </code></pre>