javascript - 在快速路由中使用 find() 时向我的 mongodb 添加过滤可能吗?如何?
问题描述
您好,我有这个使用异步的 GET 函数,它找到用户刚刚单击的特定“类别”猫鼬模式和另一个“工具”猫鼬模式(它只是在我的数据库中找到所有工具)并将它们都传输到渲染页面。
如果有一种方法可以向我的 Tool.find 添加某种过滤,我正在徘徊,因此它只会找到与用户刚刚单击的类别(Category.name)具有相同类别属性(Tool.category)的工具?
获取函数:
router.get("/catalog/:id", function (req, res, next) {
let output = {
category: [],
tools: []
};
async.parallel([
function (cb) {
Category.findById(req.params.id).exec(function (err, foundCategory) {
if (err || !foundCategory) {
req.flash("error", "No category found.");
return res.redirect("back");
} else {
output.category = foundCategory;
cb(null, foundCategory);
}
});
},
function (cb) {
Tool.find({}, function (err, foundTools) {
if (err || !foundTools) {
req.flash("error", "No tools were found.");
return res.redirect("back");
} else {
output.tools = foundTools;
cb(null, foundTools);
}
});
}
], function done(err, results) {
if (err) {
res.json(err.message);
} else {
res.render("tools/catalog-items", {
category: output.category,
tools: output.tools
});
}
});
});
解决方案
你绝对可以做到这一点。您想要实现的是使第二个查询依赖于第一个查询的结果。
在这种情况下,async.parallel
将无法正常工作,因为您需要先获得结果Category.findById
。
如果您有多个(超过 2 个)后续查询,我会建议您使用该async.waterfall
方法。您可以在此处查看文档。
但是对于这种特殊情况,您可以像这样简单:
Category.findById(req.params.id, function (err, foundCategory) {
if (err || !foundCategory) {
req.flash("error", "No category found.");
return res.redirect("back");
}
Tools.find({
// As described in the question we filter by a `name` field
// although in future you might consider creating an ObjectId reference
category: foundCategory.name
}, function (err, foundTools) {
// As we applied filtering we might return an empty array of tools
// instead of throwing an error
if (err || !foundTools) {
req.flash("error", "No tools were found.");
return res.redirect("back");
}
res.render("tools/catalog-items", {
category: foundCategory,
tools: foundTools
});
});
});
您当然也可以进行排序。您只需要添加.sort({someProperty: 1}).exec(...)
. 请参阅此处的示例。
推荐阅读
- httprequest - WinHttpRequest.5.1 和特殊字符
- java - 检测签名之间在签名 PDF 上所做的更改
- marklogic - 为什么使用字符串和结构化搜索以及开始和结束日期的组合会获得较长的响应时间?
- android - 如何在 onBindHeaderViewHolder() 方法中将点击监听器添加到 textview?
- ios - 具有可转换属性的 CoreData 在保存时失败
- javascript - 学习 Javascript:高阶函数
- visual-studio-code - VScode webview 扩展 api
- android - FLUTTER:任务':app:compileFlutterBuildDebug'执行失败无法创建父目录
- c++ - 声明与 void 不兼容
- c++ - 函数调用的参数太多,预期单个参数“nums”,有 3 个参数