What i am trying to do is the write a javascript function to access a defined articles schema in my .js file.
I have already determined that the below queries work in the mongodb terminal:
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
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?
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;
}
Q: Further, how do i convert the above javascript function to a closure function?
Note: i am using mongoose as a wrapper