title: How to make a search api in nodejs and mongodb
body: <p>I am trying to make a search api using nodejs and MongoDB. I tried to google about this and I did find something there but while trying to implement I get an error saying. I don't know how to fix this honestly I don't know anything about making search API. So any help or suggestion will be helpful for me.</p>
  
  <p>This is the link of the post I found on google <a href="https://jonathanmh.com/building-a-simple-searchable-api-with-express-backend/" rel="nofollow noreferrer">Building a simple search api</a>.</p>
  
  <blockquote>
    <p>error</p>
  </blockquote>
  
  <pre><code>{
      "error": {
          "message": "Cast to ObjectId failed for value \"search\" at path \"_id\" for model \"Post\"",
          "name": "CastError",
          "stringValue": "\"search\"",
          "kind": "ObjectId",
          "value": "search",
          "path": "_id"
      }
  }
  </code></pre>
  
  <blockquote>
    <p>This is my code </p>
  </blockquote>
  
  <pre><code>postController.search = (req, res) =&gt; {
      var response = [];
      if(typeof req.query.title !== 'undefined'){
          db.Post.filter(function(post) {
              if(post.title === req.query.title){
                  console.log(req.body);
                  response.push(post);
                  console.log(post);
              }
          });
      }
      response = _.uniqBy(response, '_id');
      if(Object.key(req.query).length === 0){
          response = db.Post
      }
      res.json(response);
  };
  </code></pre>
  
  <blockquote>
    <p>data in the collection</p>
  </blockquote>
  
  <pre><code>"data": [
      {
          "isDeleted": false,
          "_comments": [],
          "_id": "5d39122036117d2ea81b434c",
          "title": "facebook  post",
          "link": "facebook.com",
          "_creator": {
              "createdAt": "2019-07-25T01:42:21.252Z",
              "username": "adityakmr"
          },
          "createdAt": "2019-07-25T02:21:20.634Z",
          "__v": 0
      },
  ]
  </code></pre>