Guest User

Untitled

a guest
Nov 2nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. .get(function(req,res){
  2. //returning aggregate values from comments part.. comment.authorId must hv user.username.
  3. CardComment.aggregate([ //stackoverflow qn... ask tomorrow show the models
  4. //now perform the mongo join of multiple collections like in SQL Joins
  5. {
  6. $lookup:{
  7. from: 'users', //external collection.
  8. localField: 'authorId', //input field., from current collection.. cardComments
  9. foreignField: '_id', //foreign key from external collection,
  10. as: 'commentUser'
  11. }
  12. },
  13. //filter according to the cardId.. part. find()... first pipeline
  14. {
  15. $match:{"card": req.params.cardId }
  16. }
  17.  
  18. ], function(err,comments){
  19. if (err) {
  20. res.json({"success":false, "message": 'Error in loading comments'})
  21. } else {
  22. //res.json({"success": true, "message": comments})
  23. console.log(JSON.stringify(comments))
  24. //console.log(comments) //u need to ask for help on stackoverflow.
  25. }
  26. })
  27.  
  28. })
  29.  
  30. authorId:{
  31. type: ObjectId,
  32. ref : 'User',
  33. required: true
  34. },
  35. createdAt: {
  36. type: Date,
  37. default: Date.now
  38. }
  39.  
  40. var UserSchema = new Schema({
  41. username: { type: String, required: true, lowercase: true, index: { unique: true } },
  42. email: { type: String, required: true, index: { unique: true } },
  43. //password: { type: String, required: true } u forgot the select field.. that z why login was disturbing
  44. password: {type: String, select: false} //that z why u need postman to test stuff
Add Comment
Please, Sign In to add comment