获取与创建 post 的用户相匹配的用户数据
Getting data of a user that matches the user who created a post
对于标题措辞不佳,我深表歉意,我不确定如何正确措辞。所以我有 2 collections:
用户
_id:60ccb13a21d65f0c7c4c0690
username: testuser
name: test
并创建post
_id:60d80b1305dcc535c4bf111a
postTitle: "test post"
postText: "aaaaa"
postUsername: "testuser"
如果我想从他的论坛 post 上显示的用户那里获取 testuser 的数据,最好的方法是什么?这是我到目前为止尝试过的:
router.get('/forum', async (req,res)=>res.render('forum', {newPost: await Createpost.find().sort({ date: 'desc'}),
postUser: await User.find({"username": Createpost.postUsername})}));
我的尝试是在用户 collection 中搜索与创建 post collection 中的 post 用户名字段相匹配的用户名字段,然后显示它在 EJS 中:
<% newPost.forEach(newPost => { %>
Posted by: <%= newPost.postUsername %> - Name: <%= postUser.name %>
<%= newPost.postText %>
<% }%>
但是当我这样做时,页面上什么也没有出现,我也想不出任何解决办法。任何帮助将不胜感激。
编辑:我按照评论中的建议使用了聚合,但下面的结果只是显示 [object Object] 的文本,我不确定为什么:
router.get('/forum', async (req,res)=>res.render('forum', {newPost: await Createpost.find().sort({ date: 'desc'}),
postUser: Createpost.aggregate([{$lookup: { from: "User", localField: "postUsername", foreignField: "username", as: "postUser"}},{$unset: "postUsername"}])}));
EJS:
<% newPost.forEach(newPost => { %>
Posted by: <%= newPost.postUsername %> - <%= postUser %>
<%= newPost.postText %>
<% }%>
尝试聚合解决方案:
https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/
Createpost.aggregate([
{
$lookup: {
from: "User",
localField: "postUsername",
foreignField: "username",
as: "postUser"
}
},
{
$unset: "postUsername"
},
{
$sort: {date: -1}
}
])
编辑:聚合的输出数据应该是这样的:
[{
_id: 60d80b1305dcc535c4bf111a,
postTitle: "test post",
postText: "aaaaa",
//postUsername: "testuser", --> removed cuz its dupplicated with the postUser below!
postUser: {
_id: 60ccb13a21d65f0c7c4c0690,
username: "testuser",
name: "test"
},
// ... more detail of the post
}},...
]
更多:
现在你可以只用数组 newPost
渲染 ejs
router.get('/forum', async (req,res)=>res.render('forum', {
newPost: await Createpost.aggregate([
{
$lookup: {
from: "User",
localField: "postUsername",
foreignField: "username",
as: "postUser"
}
},
{
$unset: "postUsername"
},
{
$sort: {data: -1}
}
])
}));
然后将 newPost 数组解析为 ejs,如下所示:
<% newPost.forEach(newPost => { %>
Posted by: <%=newPost.postUser.username%> - Name: <%= newPost.postUser.name%>
<%= newPost.postText %>
<% }%>
对于标题措辞不佳,我深表歉意,我不确定如何正确措辞。所以我有 2 collections:
用户
_id:60ccb13a21d65f0c7c4c0690
username: testuser
name: test
并创建post
_id:60d80b1305dcc535c4bf111a
postTitle: "test post"
postText: "aaaaa"
postUsername: "testuser"
如果我想从他的论坛 post 上显示的用户那里获取 testuser 的数据,最好的方法是什么?这是我到目前为止尝试过的:
router.get('/forum', async (req,res)=>res.render('forum', {newPost: await Createpost.find().sort({ date: 'desc'}),
postUser: await User.find({"username": Createpost.postUsername})}));
我的尝试是在用户 collection 中搜索与创建 post collection 中的 post 用户名字段相匹配的用户名字段,然后显示它在 EJS 中:
<% newPost.forEach(newPost => { %>
Posted by: <%= newPost.postUsername %> - Name: <%= postUser.name %>
<%= newPost.postText %>
<% }%>
但是当我这样做时,页面上什么也没有出现,我也想不出任何解决办法。任何帮助将不胜感激。
编辑:我按照评论中的建议使用了聚合,但下面的结果只是显示 [object Object] 的文本,我不确定为什么:
router.get('/forum', async (req,res)=>res.render('forum', {newPost: await Createpost.find().sort({ date: 'desc'}),
postUser: Createpost.aggregate([{$lookup: { from: "User", localField: "postUsername", foreignField: "username", as: "postUser"}},{$unset: "postUsername"}])}));
EJS:
<% newPost.forEach(newPost => { %>
Posted by: <%= newPost.postUsername %> - <%= postUser %>
<%= newPost.postText %>
<% }%>
尝试聚合解决方案: https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/
Createpost.aggregate([
{
$lookup: {
from: "User",
localField: "postUsername",
foreignField: "username",
as: "postUser"
}
},
{
$unset: "postUsername"
},
{
$sort: {date: -1}
}
])
编辑:聚合的输出数据应该是这样的:
[{
_id: 60d80b1305dcc535c4bf111a,
postTitle: "test post",
postText: "aaaaa",
//postUsername: "testuser", --> removed cuz its dupplicated with the postUser below!
postUser: {
_id: 60ccb13a21d65f0c7c4c0690,
username: "testuser",
name: "test"
},
// ... more detail of the post
}},...
]
更多: 现在你可以只用数组 newPost
渲染 ejsrouter.get('/forum', async (req,res)=>res.render('forum', {
newPost: await Createpost.aggregate([
{
$lookup: {
from: "User",
localField: "postUsername",
foreignField: "username",
as: "postUser"
}
},
{
$unset: "postUsername"
},
{
$sort: {data: -1}
}
])
}));
然后将 newPost 数组解析为 ejs,如下所示:
<% newPost.forEach(newPost => { %>
Posted by: <%=newPost.postUser.username%> - Name: <%= newPost.postUser.name%>
<%= newPost.postText %>
<% }%>