【发布时间】:2019-02-02 02:26:56
【问题描述】:
如何获取用户的帖子及其所有关注的帖子(Mongodb、Mongoose、Nodejs)
用户架构
const userSchema = new mongoose.Schema({
firstName: { type: String, required: true, trim: true },
lastName: { type: String, required: true, trim: true },
});
userSchema.set('timestamps', true);
export default mongoose.model('user', userSchema);
追随者架构
const followSchema = new mongoose.Schema({
follower: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
following: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
status: { type: Boolean, default: true }
});
followSchema.set('timestamps', true);
export default mongoose.model('follow', followSchema);
帖子架构
const postSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
contents: { type: String, trim: true },
photo: { type: String }
});
postSchema.set('timestamps', true);
export default mongoose.model('post', postSchema);
提前感谢您! :)
【问题讨论】:
-
不清楚你在问什么。试着解释一下
-
@AnthonyWinzlet 让我举个例子来解释一下,假设我们有 3 个用户 A、B 和 C,A 都关注 B 和 C,现在我想从帖子集合中获取所有帖子,但帖子由A 和 B & C,因为 A 跟随 B & C。让我知道。如果它再次不清楚。
-
好的,所以您想获取所有由用户 A 或其追随者创建的帖子?你是什么mongodb版本?你能发布一些示例数据吗
-
@AnthonyWinzlet 是的! mongodb版本---MongoDB版本v4.0.5。您需要什么样本数据,请解释一下?只是跟随模式包含两个用户ID,我的意思是用户自己是跟随者,另一个是跟随者。
-
@spyro 我已经在阅读文档,但尚未找到解决方案,但仍然继续感谢您的帮助!