You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现单个注册用户时间线的全部帖子获取?

解决方案

首先需要修正你的Schema关联逻辑,当前数组关联设计不符合「用户-个人资料-帖子」的实际关系(一个用户对应一个个人资料、多个帖子;一个帖子属于一个用户和一个个人资料),同时需要在发帖时同步更新用户的帖子关联字段。

1. 修正Schema定义

User Schema

添加profile(关联单个个人资料)和posts(关联多个帖子)字段:

const userSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    roles: {
        User: {
            type: Number,
            default: 2001
        },
        Mentor: Number,
        Admin: Number
    },
    password: {
        type: String,
        required: true
    },
    userID: {
        type: String,
        required: true
    },
    refreshToken: String,
    // 关联单个个人资料(一对一)
    profile: {
        type: Schema.Types.ObjectId,
        ref: 'profile'
    },
    // 关联多个帖子(一对多)
    posts: [{
        type: Schema.Types.ObjectId,
        ref: 'userpost'
    }]
});

const User = mongoose.model('user', userSchema);
module.exports = User;

Profile Schema

将User字段改为单个ObjectId(一个个人资料属于一个用户):

const ProfileSchema = new Schema({
    lastname: {
        type: String,
        required: true,
    },
    firstname: {
        type: String,
        required: true,
    },
    othernames: {
        type: String,
        required: true,
    },
    countries: {
        type: String,
        required: true,
    },
    phones: {
        type: String,
        required: true,
    },  
    // 关联单个用户(一对一)
    user: {
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true,
    }
});

const Profile = mongoose.model('profile', ProfileSchema);
module.exports = Profile;

UserPost Schema

将Profile和User改为单个ObjectId(一个帖子属于一个用户和一个个人资料):

const UserpostSchema = new Schema({
    post: {
        type: String,
        required: true
    },
    // 关联单个个人资料
    profile: {
        type: Schema.Types.ObjectId,
        ref: 'profile',
        required: true,
    },
    // 关联单个用户
    user: {
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true,
    }
});

const Userpost = mongoose.model('userpost', UserpostSchema);
module.exports = Userpost;

2. 修改发帖API逻辑

创建帖子后,必须将帖子ID同步添加到对应用户的posts数组中,否则用户的帖子关联字段会一直为空:

router.post('/createpost', async (req, res) => {
    try {
        const { post, userId, profileId } = req.body;
        
        // 创建新帖子
        const newPost = new Userpost({
            post,
            user: userId,
            profile: profileId
        });
        const savedPost = await newPost.save();
        
        // 更新用户的posts数组,添加新帖子ID
        await User.findByIdAndUpdate(
            userId,
            { $push: { posts: savedPost._id } },
            { new: true } // 返回更新后的用户文档
        );
        
        res.status(201).json(savedPost);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

3. 实现获取用户时间线全部帖子的API

有两种方式可以实现:

方式一:通过User模型关联查询

router.get('/user/:userId/timeline', async (req, res) => {
    try {
        const user = await User.findById(req.params.userId)
            .populate({
                path: 'posts',
                populate: { path: 'profile' } // 同时填充帖子关联的个人资料
            });
        
        res.json(user.posts);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

方式二:直接查询Userpost模型

router.get('/user/:userId/timeline', async (req, res) => {
    try {
        const userPosts = await Userpost.find({ user: req.params.userId })
            .populate('user') // 可选:如果需要返回用户信息
            .populate('profile');
        
        res.json(userPosts);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

关键说明

  • 关联字段的命名尽量统一用单数(如user而非User),符合MongoDB的命名规范
  • 发帖时必须执行双向关联更新:创建帖子后同步更新用户的帖子数组,否则用户的posts字段无法自动填充
  • 如果是已存在的用户和帖子数据,需要手动批量更新关联关系,才能让历史数据生效

内容的提问来源于stack exchange,提问作者SwiftDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 19:25:31