【发布时间】:2021-07-05 04:43:06
【问题描述】:
我有这两个模型:
用户.js
const UserSchema = new Schema({
profile: {
type: Schema.Types.ObjectId,
ref: "profiles",
},
following: [
{
type: Schema.Types.ObjectId,
ref: "users",
},
],
});
module.exports = User = mongoose.model("users", UserSchema);
Profile.js
const ProfileSchema = new Schema({
videoURL: {
type: String,
},
});
module.exports = Profile = mongoose.model("profiles", ProfileSchema);
这是User 文档的示例:
{
"following": [
{
"profile":{
"videoURL":"video_url_1"
}
},
{
"profile":{
"videoURL":"video_url_2"
}
},
{
"profile":{}
},
{
"profile":{
"videoURL":"video_url_3"
}
},
{
"profile":{
"videoURL":"video_url_4"
}
},
{
"profile":{
"videoURL":"video_url_5"
}
},
{
"profile":{}
},
{
"profile":{
"videoURL":"video_url_6"
}
}
]
}
我正在尝试实现连接用户跟随的用户视频的无限滚动。
这意味着,我必须过滤 user.following.profile.videoURL videoURL 存在于何处
假设,我将通过两个视频加载两个视频:
- 响应 1:["video_url_1","video_url_2"]
- 响应 2:["video_url_3","video_url_4"]
- 响应 3:["video_url_5","video_url_6"]
通常,无限滚动很容易,因为我必须按存储顺序 2 x 2 加载文档而不过滤任何字段。
示例:在无限滚动中两个两个显示关注的用户
User.findById(user_id).populate({
path: "following",
options: {
skip: 2 * page,
limit: 2,
},
});
但是,现在我必须对每个 follow_user.profile.video 执行过滤,然后两个两个返回。而且我看不到如何同时执行BOTH过滤和无限滚动。
注意:根据documentation:
一般来说,没有办法根据故事作者的属性使 populate() 过滤故事。例如,以下查询不会返回任何结果,即使作者已填充。
const story = await Story.
findOne({ 'author.name': 'Ian Fleming' }).
populate('author').
exec();
story; // null
所以我想,我没有办法使用填充来过滤基于user.followers,基于每个user.follower.profile.videoURL
【问题讨论】:
-
你能解释一下你想做什么样的过滤吗?
-
我的错,我将不得不过滤 user.following.profile.videoURL WHERE videoURL 存在
标签: node.js mongodb mongoose nosql mern