【发布时间】:2021-10-08 18:15:18
【问题描述】:
我有这个架构:
const UserSchema = new Schema({
following: [
{
type: Schema.Types.ObjectId,
ref: "users",
},
],
video: {
type: Schema.Types.ObjectId,
ref: "videos",
},
});
module.exports = User = mongoose.model("users", UserSchema);
我想得到:User.following WHERE following.video EXISTS
换句话说,我想获取有视频的特定用户所关注的用户列表。
这是我到目前为止所做的:
const user = await User.aggregate([
{ $match: { _id: ObjectId(user_id) } },
{
$lookup: {
from: "users",
let: { following: "$following" },
pipeline: [{ $match: { $expr: { $in: ["$_id", "$$following"] } } }],
as: "following",
},
},
]);
const followed_users = user[0].following;
但是,我找不到根据 video 字段过滤关注用户的方法。
知道如何实现这一目标吗?
【问题讨论】:
标签: mongodb mongoose aggregation-framework mern mongoose-populate