mongoose一次调用find如何查询多个条件?
发布于 10 年前 作者 ZhangHang-z 10850 次预览 最后一次回复是 10 年前 来自 问答
有这样一个需求,需要查询7个条件,然后去渲染,但是这样的查询只有第一个生效。
exports.home = function(req, res) {
Series.find([
{},
{ "type": { "$in": [1] } },
{ "type": { "$in": [22, 20, 13] } },
{ "type": { "$in": [8, 9, 4, 13] } },
{ "type": { "$in": [15, 23, 17] } },
{ "type": { "$in": [5, 6, 7, 10, 12, 11] } },
{ "type": { "$in": [16, 18] } },
{ "type": { "$in": [21, 19, 24] } } ],
function (err, covers, one, two, three, four, five, six, seven) {
if (err) console.log(err);
covers = covers.slice(0, 9);
res.render('home', {
all: covers,
Queries: [ all, one, two, three, four, five, six, seven ]
});
});
};
如果要使用类似如下异步,如何解决深度嵌套,在全部结果后调用callback,
exports.home = function(req, res) {
Series.find({},callback);
Series.find( {"type": 1 }, callback);
Series.find( {"type": 2 }, callback);
Series.find( {"type": 3 }, callback);
Series.find( {"type": 4}, callback);
Series.find( {"type": 5}, callback);
3 回复
解决,用浦临的Nodejs深入浅出上的方法
Query#exec 返回的是Promise,可以用Promise.All
Promise.all或者aync.parallel可以的