mongodb 数据库集合查询合并问题
发布于 7 年前 作者 haibo32012 5504 次浏览 来自 问答

三个集合: collection user: schema = { userName: { type: String }, userEmail: { type: String }

} collection image: schema ={ userId: { type: String }, storePath: { type: String }, viewCount: { type: Number }, likeCount: { type: Number } }; collection subscribe: schema = { userId: { type: String }, subscribedUserId: { type: String } } 我要实现的目的是:显示所有用户订阅者发布的图片 let cursor = subscribe.find({userId: userId}) // 找到所有当前用户的订阅者 cursor.forEach(function (doc) { //用户有多个订阅者,每个订阅者发布了多张图片 image.find({userId: doc.userId}); // 这段怎么写,怎么合并多次查询同一个集合的指针? }); 在这返回所有文档的指针!

2 回复

你不会find in? const subscribedUserIdList = cursor.map(doc => doc.subscribedUserId); image.find({ userId: { $in: subscribedUserIdList }}) 根本不用循环。

@beyond5959 ,第一次用mongodb, 我试一下,谢谢!

回到顶部