【发布时间】:2021-08-06 09:30:00
【问题描述】:
我需要从数据库中检索整个单个对象层次结构作为 JSON。实际上,关于实现此结果的任何其他解决方案的建议将受到高度评价。我决定使用具有 $lookup 支持的 MongoDB。
所以我有四个收藏:
- 用户
{ "_id" : "2", "name" : "john" }
{ "_id" : "1", "name" : "Doe" }
- 帖子
{"_id": "2","body": "hello", likes: []},
{"_id": "1","name": "hello 4", likes: [1,]},
- 评论
{"_id": "c2","body": "hello 3",postId: "1",likes: [1,2]},
{"_id": "c1","body": "hello 2",postId: "1",likes: [1,2]},
- 回复
{"_id": "r1","name": "hello 4",commentId: "c1",likes: [1]},
{"_id": "r3","name": "hello five",commentId: "c2",likes: [1,2]}
我基本上需要检索所有带有所有相应 cmets 和 cmets.replies 的帖子作为我的结果的一部分。我的聚合:
const posts = await PostModel.aggregate([
{
$lookup: {
from: "comments",
localField: "_id",
foreignField: "postId",
as: "comments",
},
},
{
$unwind: "$comments",
},
{
$lookup: {
from: "replies",
localField: "comments._id",
foreignField: "commentId",
as: "comments.replies",
},
},
]).sort({
createdAt: -1,
});
结果很奇怪。有些记录没问题。但是 cmets 返回一个对象。 _id="1" 的帖子也有一些重复。
[
{
"_id": "1",
"comments": {
"_id": "c2",
"body": "hello 3",
"likes": [
1,
2
],
"postId": "1",
"replies": [
{
"_id": "r3",
"commentId": "c2",
"likes": [
1,
2
],
"name": "hello five"
}
]
},
"likes": [
1
],
"name": "hello 4"
},
{
"_id": "1",
"comments": {
"_id": "c1",
"body": "hello 2",
"likes": [
1,
2
],
"postId": "1",
"replies": [
{
"_id": "r1",
"commentId": "c1",
"likes": [
1
],
"name": "hello 4"
}
]
},
"likes": [
1
],
"name": "hello 4"
}
]
简而言之,这是我的预期结果。
- 我想获取所有帖子,以及与之关联的所有 cmets 和回复。
- 我想添加一个喜欢的计数
likesCount:{$size:["likes"]},因为我已经准备好用户身份验证 ID(uid),我想根据isLiked: {$in:[ID(uid),"likes"]}来检查用户是否喜欢该帖子、评论或回复
【问题讨论】:
-
您可能必须将其与帖子 ID mongoplayground.net/p/MaGfTdTS6v0 组合在一起
-
每条评论的回复数?
-
@Beingnin ama 将如何访问replies.likes 并统计它们
-
@Beingnin (mongoplayground.net/p/MaGfTdTS6v0)[mongoplayground.net/p/MaGfTdTS6v0]
标签: javascript php reactjs mongodb mongoose