title: How to search an element in array using node.js in mongodb?
body: <p>In mongodb there is a document like below,</p>
  <pre><code>{
     &quot;_id&quot;: ObjectId(&quot;57443657ee5b5ccc30c4e6f8&quot;),
     &quot;name&quot;: &quot;Kevin&quot;,
     &quot;email&quot;: &quot;kevinwarn@gmail.com&quot;,
     &quot;password&quot;: &quot;$2a$13$iZ0PhuY6VlBF6qC9rUredrG39Fw5pEKkIh.VCLiGsLPZMKYveyzey&quot;,
     &quot;mobile&quot;: &quot;9980896745&quot;,
     &quot;__v&quot;: NumberInt(0),
     &quot;ocassionTypes&quot;: [
       {
         &quot;occasiontype&quot;: &quot;Anniversary&quot;,
         &quot;date&quot;: &quot;2016-05-30T18:30:00.000Z&quot; 
      },
       {
         &quot;occasiontype&quot;: &quot;Donation&quot;,
         &quot;date&quot;: &quot;2016-07-24T18:30:00.000Z&quot; 
      },
       {
         &quot;occasiontype&quot;: &quot;House Warming&quot;,
         &quot;date&quot;: &quot;2016-09-21T18:30:00.000Z&quot; 
      } 
    ] 
  }
  </code></pre>
  <p>So I have written a query in <strong>Nodejs</strong> to search <code>occasiontype</code> element in <code>ocassionTypes</code> array like below,</p>
  <pre><code>router.post('/find-registry', function(req, res){
      var uEmail = req.body.email;
      var uocType = req.body.userOccasion;
      var findUserId = function(db, callback) {
          
              var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}}).toArray(function(err, docs1){
                  if(err){  
                      callback(new Error(&quot;Some problem&quot;));
                  } else {
                      callback(null,docs1);
                  } 
              });
      };
          
      MongoClient.connect(config.database, function(err, db) {
          assert.equal(null, err);
          findUserId(db, function(err,docs1) {
              db.close();
              if(err) return res.json({result:null})
              else
              return res.json({result1:docs1});
          });
      });                     
  });
  </code></pre>
  <p>Using this query I am getting 0th index element, but if I give 1st and 2nd element it always shows only 0th index in the output.</p>
  <p>In front end I have given input as shown in the picture below.</p>
  <h2>file.html</h2>
  <p><a href="https://i.stack.imgur.com/X0FB6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X0FB6.jpg" alt="front end image" /></a></p>
  <p>Is there any wrong in my query? please help me to fix this.</p>