我的MongoDB项目中有三个集合。一个集合包括用户,另一个集合用于特定于用户的帐户,最后一个集合是一些活动。
我想获得基于用户特定平台的活动。
活动文档如下所示。
[{
id: "401",
maxfollowers: "645963",
minfollowers: "2665",
name: "test 12",
platform: "Youtube",
status: "Active",
userId: "123"
},
{
id: "402",
maxfollowers: "6202",
minfollowers: "500",
name: "test 123",
platform: "Toktok",
status: "Active",
userId: "123"
}]帐户文档如下所示。
[{
id: "501",
followers: "6000",
platform: "Youtube",
userId: "123"
},
{
id: "502",
followers: "500",
platform: "Toktok",
userId: "123"
}]在这里,Id为123的用户有两个帐户。基于用户账号平台和追随者,我想编写一个查询来获取活动列表。
下面是我尝试过的代码。
Account.find({userId})
.then(result => {
console.log(result)
const platforms = [];
result.forEach(item => {
// frame the query here.
});
Advertisement.find(search)
.then(response => {
res.status(200).json({
data: response
});
})
.catch(err => {
errorHandler(res, err);
});
})
.catch(err => {
errorHandler(res, err);
});我使用的是NodeJs,express js和mongoose。
发布于 2020-10-02 22:15:56
我建议使用aggregate和$lookup来连接集合,请参阅MongoDB $lookup pipeline match by _id not working
https://stackoverflow.com/questions/64172804
复制相似问题