title: How to retrieve array of specific field of sub document- mongodb
body: <p>This is my first mongodb project,I have this document structure in mongodb, I am trying to retrieve a particular user account (each user account has an array of contacts), from this user account, I will then obtain an array of the ID fields of the users contacts and then pass this array as a parameter to another query, I am doing this to avoid having to loop through the users contacts array in order to obtain the ID fields, here is the document structure, the query I tried is below it</p>
  
  <pre><code>{
    name,
    id,
  
    contacts:[{
               contactId, //I need an array of this field
               dateAdded
               },
               contactId,
               dateAdded
               },
                {}..]
  }
  
  //
    var findByIdAll = function(accountId, callback) {
       var self=this;
            //Get the user account
        Account.findOne({_id:accountId}, function(err,doc) {
  
            /  After the user account has been obtained, the function below will
          // use an array of the users contactsId's to fetch the contact's accounts
            //please how do I obtain the array of contact Id's before reaching here                     
  
         self.Account.find({_id:{$in:[/array of contact Ids]}},function(err,results){
        callback(results);
      });
      });
    };
  </code></pre>
  
  <p>EDIT
  //I have now been able to obtain an array of contactID fields using the following query</p>
  
  <pre><code> var r=db.accounts.aggregate({$match:{email:'m@live.com'}},{$unwind:"$contacts"},
         {$project:{_id:0,contacts:1}},{$group:{_id:'$_id',
         list:{$push:'$contacts.accountId'}}});
  
      The result I get from the query is 
  </code></pre>
  
  <blockquote>
    <p>r</p>
  </blockquote>
  
  <pre><code>{
          "result" : [
                  {
                          "_id" : null,
                          "list" : [
                                  ObjectId("51c59a31c398c40c22000004"),
                                  ObjectId("51c59a31c398c40c22000004")
                          ]
                  }
          ],
          "ok" : 1
  }
  </code></pre>