Javascript 基于if I';我在跟踪用户

Javascript 基于if I';我在跟踪用户,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我如何向我的登录用户显示他们正在跟踪的人的帖子列表 我有一个post模式(为了简单起见,将其删减): 和我的跟随者模式(也减少): 如果post.user等于follower.followerid(假设follower.userId也等于我登录的用户),我如何找到最新的帖子 迄今为止的尝试: 我已经花了几个小时在这上面,我可以从文章中提取结果,然后在Node.js中过滤它们,但这在大规模上是没有效率的 以下是一个不起作用的查找查询: 有什么建议可以让它工作吗 这可能是一个更好的解决方案,我可以

我如何向我的登录用户显示他们正在跟踪的人的帖子列表

我有一个post模式(为了简单起见,将其删减):

和我的跟随者模式(也减少):

如果
post.user
等于
follower.followerid
(假设
follower.userId
也等于我登录的用户),我如何找到最新的帖子


迄今为止的尝试:

  • 我已经花了几个小时在这上面,我可以从文章中提取结果,然后在Node.js中过滤它们,但这在大规模上是没有效率的

  • 以下是一个不起作用的查找查询:

  • 有什么建议可以让它工作吗

  • 这可能是一个更好的解决方案,我可以:

    • 加载我关注的所有用户,并获取他们的ID
    • 加载
      userId
      等于数组中任何userId的所有帖子
  • 试试这个:

    const followings = await Follower.find({ userId: req.userId });
    const followingIds = followings.map(f => f.followingId);
    const posts = await Post.find({ userId: { $in: followingIds } });
    
    顺便说一句,使用
    aggregate
    query是一个更好的解决方案。尝试更改此选项:

        $lookup:
            {
                from: "follower",
                localField: "userId",
                foreignField: "following",
                as: "relationship"
            }
    
    致:


    我认为外域应该是“followId”。

    根据technophyle的回答,我将
    从:“follower”
    更新为
    从:“followers”

    我还需要访问$match中数组的第一个值,而不是根(需要添加
    0

    {“$match”:{“relationship.0.userId”:req.userId}

    const posts = await Post.aggregate([
        {
            $lookup:
                {
                    from: "followers",
                    localField: "userId",
                    foreignField: "following",
                    as: "relationship"
                }
        },
        { "$match": { "relationship.0.userId": req.userId} }
    ]);
    
    res.send({posts});
    

    对不起,是的,
    followind
    在我的代码中是
    following
    。在本例中,我只是将其命名为
    followID
    ,以使其更清晰。我已经更新了我的问题。但是,问题仍然存在。userId和followId是字符串类型。你试过使用ObjectId吗?正如我对Sylvain说的,我的代码中有followID。在本例中,我只是将其命名为followID,以使其更清晰。我已经更新了我的问题。但是,这个问题仍然没有解决。您注意到我在
    from
    字段中使用了复数形式(实际的数据库集合名称),对吗?啊!我没有注意到。我会试试看。谢谢完美的谢谢!我通过从“followers”添加复数形式
    ,并通过更新匹配来加载
    关系.0.userId
    而不是
    关系.userId
    。还有@technophyle you legend。你写出了我的第三次尝试,效果也很好。在性能方面$查找解决方案需要55毫秒到60毫秒。而加载用户然后发布解决方案需要29ms-32ms。想知道哪种选择在更大范围内更好?
    const followings = await Follower.find({ userId: req.userId });
    const followingIds = followings.map(f => f.followingId);
    const posts = await Post.find({ userId: { $in: followingIds } });
    
        $lookup:
            {
                from: "follower",
                localField: "userId",
                foreignField: "following",
                as: "relationship"
            }
    
        $lookup:
            {
                from: "followers",
                localField: "userId",
                foreignField: "followingId",
                as: "relationship"
            }
    
    const posts = await Post.aggregate([
        {
            $lookup:
                {
                    from: "followers",
                    localField: "userId",
                    foreignField: "following",
                    as: "relationship"
                }
        },
        { "$match": { "relationship.0.userId": req.userId} }
    ]);
    
    res.send({posts});