title: How to query for sub-document in an array with Mongoose
body: <p>I have a Schema of Project that looks like this:</p>
  <pre><code>const ProjectSchema = new mongoose.Schema({
  name: {
      type: String,
      Required: true,
      trim: true
  },
  description: {
      type: String,
  },
  
  devices: [{
      name: {type: String, Required: true},
      number: {type: String, trim: true},
      deck: {type: String},
      room: {type: String},
      frame: {type: String}
  }],
  
  cables: {
      type: Array
  },
  user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
  },
  adminsID: {
      type: Array
  },
  createdAt: {
      type: Date,
      default: Date.now
  }
  </code></pre>
  <p>I want to query an object from array of &quot;devices&quot;.
  I was able to add, delete and display all sub-documents from this array but I found it really difficult to get single object that matches _id criteria in the array.</p>
  <p>The closest I got is this (I'm requesting: '/:id/:deviceID/edit' where &quot;:id&quot; is Project ObjectId.</p>
  <pre><code>let device = await Project.find(&quot;devices._id&quot;: req.params.deviceID).lean()
  console.log(device)
  </code></pre>
  <p>which provides me with below info:</p>
  <pre><code>[
  {
  _id: 6009cfb3728ec23034187d3b,
  cables: [],
  adminsID: [],
  name: 'Test project',
  description: 'Test project description',
  user: 5fff69af08fc5e47a0ce7944,
  devices: [ [Object], [Object] ],
  createdAt: 2021-01-21T19:02:11.352Z,
  __v: 0
  }
  ]
  </code></pre>
  <p>I know this might be really trivial problem, but I have tested for different solutions and nothing seemed to work with me. Thanks for understanding</p>