title: Search an item with mongoose
body: <p>I want to search for a specific item from the database(mongodb) using mongoose ODM and display the item in my view. This is what I have found from the internet but its not working. Here is my controller:</p>
  <pre><code>exports.getSearch = (req, res, next) =&gt; { 
    const { name } = req.query;
    Product.find({title: { $regex: name, $options: &quot;i&quot; }})
      .then(title  =&gt; {
        res.render('shop/product-list', {
          prods:  title ,
          pageTitle: 'All Products',
          path: '/products'  
        });
      })
      .catch(err =&gt; {
        console.log(err);
      });
  }
  </code></pre>
  <p>When I run my server I get this error after trying to search:</p>
  <blockquote>
  <p>CastError: Cast to ObjectId failed for value &quot;search&quot; (type string) at
  path &quot;_id&quot; for model &quot;Product&quot;</p>
  </blockquote>
  <p>Here is my search form:</p>
  <pre><code>&lt;form action=&quot;/products&quot; method=&quot;POST&quot;&gt;
                      &lt;input type=&quot;text&quot; placeholder=&quot;search product&quot; name=&quot;name&quot;&gt;
                      &lt;button type=&quot;submit&quot;&gt;Search&lt;/button&gt;
                  &lt;/form&gt;
  </code></pre>
  <p>Sample of the fields in my mongodb:</p>
  <pre><code>_id: 628398cb487a2cf1538c4087
  title: &quot;Dell  E7240&quot;
  price: 28600
  description: &quot;Dell Latitude E7240
  Core i5 4th gen
  12'
  4GB RAM 
  120GB SSD&quot;
  imageUrl: &quot;images/2022-05-17T12:44:58.743Z-e7250.jpg&quot;
  userId: 627540c6672b6ab4007a3856
  __v: 0
  </code></pre>
  <p>My product Schema:</p>
  <pre><code>const productSchema = new Schema({
    title: {
      type: String,
      required: true
    },
    price: {
      type: Number,
      required: true
    },
    description: {
      type: String,
      required: true
    },
    imageUrl: {
      type: String,
      required: true
    },
    userId: {
      type: Schema.Types.ObjectId,
      ref: 'User',
      required: true
    }
  });
  </code></pre>