【发布时间】:2019-06-18 23:02:09
【问题描述】:
我正在为一个应用程序开发一个 nodejs 后端,在该应用程序中,用户将能够看到他/她关注的用户创建的帖子。
我想用猫鼬来做这个。
我的用户架构:
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
hashed_password: {
type: String,
required: true
},
isVerified: {
type:Boolean,
default: false
},
emailVerificationToken: String,
emailVerificationTokenExpires: Date,
followers: [{ type: ObjectId, ref: 'User'}],
following: [{ type: ObjectId, ref: 'User'}],
posts: [{type: ObjectId, ref: 'Post'}],
bio: String,
website: String
});
我的帖子架构:
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
url: {
type: String,
required: true
},
description: {
type: String,
},
website: String,
tags : [String],
createdBy: { type: ObjectId, ref: 'User'}
});
我试过这个:
User.findById(currentUserId)
.populate("following","posts")
.populate("following.posts","_id title")
.skip()
.limit()
.exec(function(err,users){
res.send(users);
});
这是返回带有关注者和已填充帖子的分页用户。我只想要所有用户的帖子。谁能帮我解决这个问题?
【问题讨论】:
-
如果您只是填充帖子 -
.populate("posts")会发生什么?
标签: node.js mongoose pagination