mongodb嵌套对象ID

发布于 2025-02-10 11:45:14 字数 1245 浏览 2 评论 0原文

嗨,我正在尝试构建一个社交应用程序,用户可以互相关注我的用户模式如下:

{
      _id: ObjectId('62b6a1ad5bb65d5a1e2c0dcc'),
      name: 'Admin',
      username: 'Admin',
      email: '[email protected]',
      password: '$2b$10$qVWp8HZdId/cjVOXwiNLyeyv0jNo7BMFqFEKwNQniusazABp6s216',
      profilePicture: '',
      coverPicture: '',
      followers: [
        ObjectId('62b6abc96e3985aa5b2848b8'),
        ObjectId('62b6abb96e3985aa5b2848b0'),
        ObjectId('62b6abbf6e3985aa5b2848b2'),
      ],
      following: [],
      isAdmin: false,
      createdAt:  2022-06-25T05:48:29.095Z,
      updatedAt:  2022-06-25T07:45:33.290Z,
      __v: 0,
}

post架模式

{
      _id: ObjectId('62b6eaba03ca17b3eb642e24'),
      userId: ObjectId('62b6a1ad5bb65d5a1e2c0dcc'),
      description: 'one',
      image:'https://firebasestorage.googleapis.com/v0......',
      tags: [],
      likes: [],
      createdAt: '2022-06-25T11:00:10.108Z',
      updatedAt: '2022-06-25T11:00:10.108Z',
      __v: 0,
}

关注者,跟随,喜欢的是ObjectID的数组。 我想为用户获取时间轴帖子,其中包括该用户的帖子和他的以下帖子。在Nodejs服务器中,如果没有RunInng循环,我如何实现此结果。

Hi I am trying to build a social app where user can follow each other my user schema looks like this :

{
      _id: ObjectId('62b6a1ad5bb65d5a1e2c0dcc'),
      name: 'Admin',
      username: 'Admin',
      email: '[email protected]',
      password: '$2b$10$qVWp8HZdId/cjVOXwiNLyeyv0jNo7BMFqFEKwNQniusazABp6s216',
      profilePicture: '',
      coverPicture: '',
      followers: [
        ObjectId('62b6abc96e3985aa5b2848b8'),
        ObjectId('62b6abb96e3985aa5b2848b0'),
        ObjectId('62b6abbf6e3985aa5b2848b2'),
      ],
      following: [],
      isAdmin: false,
      createdAt:  2022-06-25T05:48:29.095Z,
      updatedAt:  2022-06-25T07:45:33.290Z,
      __v: 0,
}

Post schema

{
      _id: ObjectId('62b6eaba03ca17b3eb642e24'),
      userId: ObjectId('62b6a1ad5bb65d5a1e2c0dcc'),
      description: 'one',
      image:'https://firebasestorage.googleapis.com/v0......',
      tags: [],
      likes: [],
      createdAt: '2022-06-25T11:00:10.108Z',
      updatedAt: '2022-06-25T11:00:10.108Z',
      __v: 0,
}

Followers , Following , likes are the array of objectId .
I want to fetch timeline post for user it include the post of that users and post of his following . How I can achive this result without runinng loops in the nodejs server .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2025-02-17 11:45:15

使用$查找获得匹配的帖子
结果将添加到字段中,然后跟随post

尝试 Playground

db.user.aggregate(
[{
    $lookup: {
        from: 'post',
        localField: '_id',
        foreignField: 'userId',
        as: 'my-post'
    }
}, {
    $lookup: {
        from: 'post',
        localField: 'followers',
        foreignField: 'userId',
        as: 'follow-post'
    }
}]
)

use $lookup for get matched post
the result will be added to field my-post and follow-post

try playground

db.user.aggregate(
[{
    $lookup: {
        from: 'post',
        localField: '_id',
        foreignField: 'userId',
        as: 'my-post'
    }
}, {
    $lookup: {
        from: 'post',
        localField: 'followers',
        foreignField: 'userId',
        as: 'follow-post'
    }
}]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文