【发布时间】:2021-04-04 22:01:49
【问题描述】:
我遇到了一个问题,未能找到我正在寻找的内容。
我也有这个 Ads 架构和其他人的参考资料:
link: {
type: String,
default: 'n/a',
},
page: {
type: mongoose.Schema.ObjectId,
ref: 'AdsPage',
required: true,
},
slot: {
type: mongoose.Schema.ObjectId,
ref: 'AdsSlot',
required: true,
},
我想通过在page 属性上应用条件来获取数据,page 是一个包含 url 属性的模式。
页面架构:
{
title: {
type: String,
required: [true, 'Please add page title'],
unique: true,
trim: true,
maxlength: [50, 'Name cannot be more then 50 characters'],
},
url: {
type: String,
required: [true, 'Add page URL'],
},
createdAt: {
type: Date,
default: Date.now,
},
},
我想获取与提供的页面网址匹配的所有广告。
我的查询如下:
if (req.params.pageUrl) {
const ads = await Ads.find({
'page.url': req.params.pageUrl,
});
return res.status(200).json({
success: true,
message: 'Successfully fetched ads for specific page',
count: ads.length,
data: ads,
});
}
参数中的页面网址很好,但不知何故,这个过滤器不起作用,我没有错误但结果为零。
我尝试了$match 属性,但出现了一些上层错误。
非常感谢有关查询嵌套 ref 对象的任何帮助。
【问题讨论】:
标签: node.js mongodb mongoose mean-stack mongoose-schema