title: mongodb query into javascript function
body: <p>What i am trying to do is the write a javascript function to access a defined <code>articles</code> schema in my <code>.js</code> file. </p>
  
  <p>I have already determined that the below queries work in the mongodb terminal:</p>
  
  <pre><code>    db.articles.ensureIndex( { "comments.user_id" : 1 } )
          db.articles.find( { "comments.user_id" : 987654 } ) // returns all document fields, meaning X and Y including comments
  
          db.articles.find( { "comments.user_id" : 987654 }, 
  { "title" : 1, "comments.user_id" : 1 })   //some trimming
  </code></pre>
  
  <p>The purpose of the javascript function is to retrieve all comments made by a specific user, is my below attempt correct corresponding to the above mongodb queries? Are the style, syntax considered good practice?</p>
  
  <pre><code>exports.allCommentsByUser = function(userId){ 
     db.articles.ensureIndex({"comments.user_id" : 1})
      var allComments = db.articles.find({"comments.user_id" : userId}, 
                    { "title" : 1, "comments.user_id" : 1 });
      return allComments;
  }
  </code></pre>
  
  <p><strong>Q: Further, how do i convert the above javascript function to a closure function?</strong></p>
  
  <p>Note: i am using <code>mongoose</code> as a wrapper</p>