mongodb 如何获取用户时间线上的用户帖子?

brvekthn  于 2022-11-22  发布在  Go
关注(0)|答案(1)|浏览(142)

我有一个包含3个集合的数据库[Users(包含注册用户),Userpost(包含注册用户的帖子),而Profile(包含他们的个人配置文件)]。我可以使用他们的ObjectId将Profile和Users方案链接到Userpost方案。
目前我所知道的是如何获取用户帖子并填充用户和配置文件。我想做的是获取单个注册用户的所有帖子他/她的时间线。我尝试通过ObjectId将用户帖子和配置文件模式添加到UserSchema中,但每次我发布帖子时,用户集合中的用户帖子总是一个空数组。
下面是我的架构

用户方案

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
});

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

module.exports = User;

配置文件架构

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;

用户发布方案

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;

如何将用户和配置文件填充到API上的UserPost

router.get('/getpost/:id', (req, res) => {
    const id = req.params.id;
    Userpost.find({_id:id}).populate('User').populate('Profile').exec((err,docs) => {
        if(err) throw(err);
        res.json(docs);
    })
});

我如何获取用户的整个帖子到他的时间线?
请帮忙。
谢谢并致以问候

huwehgph

huwehgph1#

这应该适用于u,在UserPost模式中,你已经为Profile和User取了数组,这是不需要的,而且在模型中的每一个地方都使用了mongoose.schema而不是Schema。

用户方案

const mongoose = require('mongoose')
const userSchema = new mongoose.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
});

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

module.exports = User;

配置文件架构

const mongoose = require("mongoose");
const ProfileSchema = new mongoose.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: mongoose.Schema.Types.ObjectId,
    ref: "user",
    required: true,
},
});

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

module.exports = Profile;

用户发布方案

const mongoose = require("mongoose");
const UserpostSchema = new mongoose.Schema({
  post: {
    type: String,
    required: true,
  },
  Profile: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "profile",
    required: true,
  },
  User: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
    required: true,
  },
});

const Userpost = mongoose.model("userpost", UserpostSchema);

module.exports = Userpost;

相关问题