title: How to query a single embedded document in an array in MongoDB?
body: <p>I am trying to query a single embedded document in an array in MongoDB. I don't know what I am doing wrong. Programmatically, I will query this document and insert new embedded documents into the currently empty <code>trips</code> arrays. </p>
  
  <pre><code>{
      "_id" : ObjectId("564b3300953d9d51429163c3"),
      "agency_key" : "DDOT",
      "routes" : [
          {
              "route_id" : "6165",
              "route_type" : "3",
              "trips" : [ ]
          },
          {
              "route_id" : "6170",
              "route_type" : "3",
              "trips" : [ ]
          },
  ...
  
      ]
  }
  </code></pre>
  
  <p>Following queries -I run in mongo shell- return empty:</p>
  
  <pre><code>db.tm_routes.find( { routes :  {$elemMatch: { route_id:6165 } } } ).pretty();
  
  db.tm_routes.find( { routes :  {$elemMatch: { route_id:6165,route_type:3 } } } ).pretty();
  
  db.tm_routes.find({'routes.route_id':6165}).pretty()
  </code></pre>
  
  <p>also <code>db.tm_routes.find({'routes.route_id':6165}).count()</code> is <code>0</code>.</p>
  
  <p>The following query returns every document in the array</p>
  
  <pre><code>db.tm_routes.find({'routes.route_id':'6165'}).pretty();
  
  {
      "_id" : ObjectId("564b3300953d9d51429163c3"),
      "agency_key" : "DDOT",
      "routes" : [
          {
              "route_id" : "6165",
              "route_type" : "3",
              "trips" : [ ]
          },
          {
              "route_id" : "6170",
              "route_type" : "3",
              "trips" : [ ]
          },
          ...
          ]}
  </code></pre>
  
  <p>but <code>db.tm_routes.find({'routes.route_id':'6165'}).count()</code> returns 1.</p>
  
  <p>And finally, here is how I inserted data in the first place -in Node.JS-:</p>
  
  <pre><code>async.waterfall([
  ...
  //RETRIEVE ALL ROUTEIDS FOR EVERY AGENCY
          function(agencyKeys, callback) {
            var routeIds = [];
            var routesArr = [];
            var routes = db.collection('routes'); 
  //CALL GETROUTES FUNCTION FOR EVERY AGENCY
            async.map(agencyKeys, getRoutes, function(err, results){
              if (err) throw err;
              else {
                callback(null, results);
              }
            });
  //GET ROUTE IDS
            function getRoutes(agencyKey, callback){
              var cursor = routes.find({agency_key:agencyKey});
              cursor.toArray(function(err, docs){
                if(err) throw err;
                for(i in docs){
                  routeIds.push(docs[i].route_id);
                  var routeObj = {
                    route_id:docs[i].route_id,
                    route_type:docs[i].route_type,
                    trips:[]
                  };
                  routesArr.push(routeObj);
      /* I TRIED 3 DIFFERENT WAYS TO PUSH DATA
  //1-&gt;
  
  
          collection.update({agency_key:agencyKey}, {$push:{"routes":{
                    'route_id':docs[i].route_id,
                    'route_type':docs[i].route_type,
                    'trips':[]
                  }}});
      //2-&gt;
  
  collection.update({agency_key:agencyKey}, {$push:{"routes":routeObj}});
  
      */
                    }
      // 3-&gt;
                    collection.update({agency_key:agencyKey}, {$push:{routes:{$each:routesArr}}});
                    callback(null, routeIds);
                  });
                };
              },
  ...
  
      var collection = newCollection(db, 'tm_routes',[]);
      function newCollection(db, name, options){
        var collection = db.collection(name);
        if (collection){
          collection.drop();
        }
        db.createCollection(name, options);
        return db.collection(name);
      }
  </code></pre>
  
  <p>Note: I am not using Mongoose and don't want to use if possible.</p>