Guest User

Untitled

a guest
May 9th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. {
  2. "_id" : ObjectId("5ad5ddb15e540442a7d4213c"),
  3. "updatedAt" : ISODate("2018-05-09T10:13:24.107Z"),
  4. "createdAt" : ISODate("2018-04-17T11:42:41.027Z"),
  5. "title" : "111111111111111111111111111111",
  6. "content" : "222222222222222222222222222222222222222222",
  7. "author" : ObjectId("5ac8ba3582c2345af70d4658"),
  8. "comments" : [
  9. ObjectId("5af2a10dc56ad8378a3fbffa"),
  10. ObjectId("5af2a87b370d32a64d34efb4")
  11. ],
  12. "images" : [],
  13. "__v" : 0
  14. }
  15.  
  16. {
  17. "_id" : ObjectId("5af2a10dc56ad8378a3fbffa"),
  18. "likeBy" : [
  19. ObjectId("5ac8ba3582c2345af70d4658")
  20. ],
  21. "dislikeBy" : [],
  22. "author" : ObjectId("5ac8ba3582c2345af70d4658"),
  23. "post" : ObjectId("5ad5ddb15e540442a7d4213c"),
  24. "comment" : "222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
  25. "type" : "post",
  26. "createdAt" : ISODate("2018-05-09T07:19:41.895Z"),
  27. "updatedAt" : ISODate("2018-05-09T10:13:08.431Z"),
  28. "__v" : 0
  29. }
  30. {
  31. "_id" : ObjectId("5af2a87b370d32a64d34efb4"),
  32. "likeBy" : [],
  33. "dislikeBy" : [
  34. ObjectId("5ac8ba3582c2345af70d4658")
  35. ],
  36. "author" : ObjectId("5ac8ba3582c2345af70d4658"),
  37. "post" : ObjectId("5ad5ddb15e540442a7d4213c"),
  38. "comment" : "444444444444444444444444",
  39. "type" : "post",
  40. "createdAt" : ISODate("2018-05-09T07:19:41.895Z"),
  41. "updatedAt" : ISODate("2018-05-09T10:13:12.087Z"),
  42. "__v" : 0
  43. }
  44.  
  45. {
  46. "_id" : ObjectId("5ac8ba3582c2345af70d4658"),
  47. "firstName" : "Bruce",
  48. "lastName" : "Wayne",
  49. "email" : "[email protected]"
  50. }
  51.  
  52. const post = await Post.aggregate([
  53. { $match: { _id: mongoose.Types.ObjectId(id) }},
  54. { $lookup: {
  55. from: 'comments',
  56. localField: 'comments',
  57. foreignField: '_id',
  58. as: 'comments'
  59. }
  60. },
  61. { $lookup: {
  62. from: 'users',
  63. localField: 'author',
  64. foreignField: '_id',
  65. as: 'author'
  66. }
  67. },
  68. { $unwind: '$author' },
  69. { $unwind: '$comments' },
  70. { $addFields: {
  71. "comments.isLiked": {
  72. $in: [
  73. mongoose.Types.ObjectId(req.user.id),
  74. "$comments.likeBy"
  75. ]
  76. },
  77. "comments.isDisliked": {
  78. $in: [
  79. mongoose.Types.ObjectId(req.user.id),
  80. "$comments.dislikeBy"
  81. ]
  82. },
  83. "comments.likeByCount": { $size: '$comments.likeBy' },
  84. "comments.dislikeByCount": { $size: '$comments.dislikeBy' }
  85. }},
  86. { $lookup: {
  87. from: 'users',
  88. localField: 'comments.author',
  89. foreignField: '_id',
  90. as: 'comments.author'
  91. }
  92. },
  93. { $unwind: '$comments.author' },
  94. { $group: {
  95. _id: '$_id',
  96. updatedAt: {$first: '$updatedAt'},
  97. createdAt: {$first: '$createdAt'},
  98. title: {$first: '$title'},
  99. content: {$first: '$content'},
  100. author: {$first: '$author'},
  101. comments: {$push: '$comments'},
  102. images: {$first: '$images'}
  103. }
  104. },
  105. { $project: {
  106. updatedAt: 1, createdAt: 1, title: 1, content: 1, author: '$author', comments: 1, images: 1
  107. }
  108. }
  109. ])
Add Comment
Please, Sign In to add comment