Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id" : ObjectId("5ad5ddb15e540442a7d4213c"),
- "updatedAt" : ISODate("2018-05-09T10:13:24.107Z"),
- "createdAt" : ISODate("2018-04-17T11:42:41.027Z"),
- "title" : "111111111111111111111111111111",
- "content" : "222222222222222222222222222222222222222222",
- "author" : ObjectId("5ac8ba3582c2345af70d4658"),
- "comments" : [
- ObjectId("5af2a10dc56ad8378a3fbffa"),
- ObjectId("5af2a87b370d32a64d34efb4")
- ],
- "images" : [],
- "__v" : 0
- }
- {
- "_id" : ObjectId("5af2a10dc56ad8378a3fbffa"),
- "likeBy" : [
- ObjectId("5ac8ba3582c2345af70d4658")
- ],
- "dislikeBy" : [],
- "author" : ObjectId("5ac8ba3582c2345af70d4658"),
- "post" : ObjectId("5ad5ddb15e540442a7d4213c"),
- "comment" : "222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
- "type" : "post",
- "createdAt" : ISODate("2018-05-09T07:19:41.895Z"),
- "updatedAt" : ISODate("2018-05-09T10:13:08.431Z"),
- "__v" : 0
- }
- {
- "_id" : ObjectId("5af2a87b370d32a64d34efb4"),
- "likeBy" : [],
- "dislikeBy" : [
- ObjectId("5ac8ba3582c2345af70d4658")
- ],
- "author" : ObjectId("5ac8ba3582c2345af70d4658"),
- "post" : ObjectId("5ad5ddb15e540442a7d4213c"),
- "comment" : "444444444444444444444444",
- "type" : "post",
- "createdAt" : ISODate("2018-05-09T07:19:41.895Z"),
- "updatedAt" : ISODate("2018-05-09T10:13:12.087Z"),
- "__v" : 0
- }
- {
- "_id" : ObjectId("5ac8ba3582c2345af70d4658"),
- "firstName" : "Bruce",
- "lastName" : "Wayne",
- "email" : "[email protected]"
- }
- const post = await Post.aggregate([
- { $match: { _id: mongoose.Types.ObjectId(id) }},
- { $lookup: {
- from: 'comments',
- localField: 'comments',
- foreignField: '_id',
- as: 'comments'
- }
- },
- { $lookup: {
- from: 'users',
- localField: 'author',
- foreignField: '_id',
- as: 'author'
- }
- },
- { $unwind: '$author' },
- { $unwind: '$comments' },
- { $addFields: {
- "comments.isLiked": {
- $in: [
- mongoose.Types.ObjectId(req.user.id),
- "$comments.likeBy"
- ]
- },
- "comments.isDisliked": {
- $in: [
- mongoose.Types.ObjectId(req.user.id),
- "$comments.dislikeBy"
- ]
- },
- "comments.likeByCount": { $size: '$comments.likeBy' },
- "comments.dislikeByCount": { $size: '$comments.dislikeBy' }
- }},
- { $lookup: {
- from: 'users',
- localField: 'comments.author',
- foreignField: '_id',
- as: 'comments.author'
- }
- },
- { $unwind: '$comments.author' },
- { $group: {
- _id: '$_id',
- updatedAt: {$first: '$updatedAt'},
- createdAt: {$first: '$createdAt'},
- title: {$first: '$title'},
- content: {$first: '$content'},
- author: {$first: '$author'},
- comments: {$push: '$comments'},
- images: {$first: '$images'}
- }
- },
- { $project: {
- updatedAt: 1, createdAt: 1, title: 1, content: 1, author: '$author', comments: 1, images: 1
- }
- }
- ])
Add Comment
Please, Sign In to add comment