【问题标题】:Getting data of a user that matches the user who created a post获取与创建帖子的用户匹配的用户数据
【发布时间】:2021-06-30 09:48:37
【问题描述】:

对于措辞不当的标题,我深表歉意,我不知道如何正确措辞。所以我有 2 个收藏:

用户

_id:60ccb13a21d65f0c7c4c0690
username: testuser
name: test

然后创建帖子

_id:60d80b1305dcc535c4bf111a
postTitle: "test post"
postText: "aaaaa"
postUsername: "testuser"

如果我想从他的论坛帖子上显示的用户那里获取 testuser 的数据,那么最好的方法是什么?这是我迄今为止尝试过的:

router.get('/forum', async (req,res)=>res.render('forum', {newPost: await Createpost.find().sort({ date: 'desc'}), 
postUser: await User.find({"username": Createpost.postUsername})}));

我的尝试是在 User 集合中搜索与 Createpost 集合中的 postUsername 字段匹配的用户名字段,然后在 EJS 中显示:

<% newPost.forEach(newPost => { %>
    Posted by: <%= newPost.postUsername %> - Name: <%= postUser.name %>
    <%= newPost.postText %>
<% }%>

但是当我这样做时,页面上什么也没有出现,我想不出任何解决方案。任何帮助将不胜感激。

编辑:我使用了 cmets 中建议的聚合,但以下只是导致显示 [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 %>
<% }%>

【问题讨论】:

  • 方法是使用聚合查询通过username“加入”两个集合。见$lookup

标签: javascript node.js mongodb express ejs


【解决方案1】:

尝试聚合解决方案: 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 %>
<% }%>

【讨论】:

  • 感谢您的回复!我尝试了这个解决方案,但它似乎没有将新加入的字段作为对象返回,所以我很难在 ejs 中使用聚合数据。有什么建议吗?
  • @ChanKim 我在输出中添加了更多细节。可以在此基础上重新配置如何解析ejs中的数据!
  • 嗨,感谢所有帮助,但是当我尝试在 EJS 中使用它时,它只会返回 [object Object],我不知道为什么。如果您想看的话,我编辑了我的原始帖子,非常感谢您
  • @ChanKim 当你像上面那样聚合时,你只需要发送 newPost 因为现在 userPost 嵌入在 newPost 元素中。我在下面添加了更多细节,向您展示如何将输出解析为 ejs :-)
  • 是的,尝试将其更改为用户,它可能会解决您的问题;)
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
相关资源
最近更新 更多