mongoose一次调用find如何查询多个条件?
发布于 10天前 作者 ZhangHang-z 295 次浏览 来自 问答

有这样一个需求,需要查询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深入浅出上的方法

exports.home = function(req, res) {
  Series.find({}, null, { limit: 10 }, function (err, covers) {
      if (err) console.log('轮播图查询查询出错: ' + err);
      emitter.emit('done', 'covers', covers);
    });


  Series.find({ "type": { "$in": [1, 2, 3, 14] } }, function (err, one) {
    if (err) console.log('one查询查询出错: ' + err);
    emitter.emit('done', 'one', one);
  });


  Series.find({ "type": { "$in": [22, 20, 13] } }, function (err, two) {
    if (err) console.log('two查询查询出错: ' + err);
    emitter.emit('done', 'two', two);
  });


  Series.find({ "type": { "$in": [8, 9, 4, 13] } }, function (err, three) {
    if (err) console.log('three查询查询出错: ' + err);
    emitter.emit('done', 'three', three);
  });


  Series.find({ "type": { "$in": [15, 23, 17] } }, function (err, four) {
    if (err) console.log('four查询查询出错: ' + err);
    emitter.emit('done', 'four', four);
  });


  Series.find({ "type": { "$in": [5, 6, 7, 10, 12, 11] } }, function (err, five) {
    if (err) console.log('five查询查询出错: ' + err);
    emitter.emit('done', 'five', five);
  });


  Series.find({ "type": { "$in": [16, 18, 23] } }, function (err, six) {
    if (err) console.log('six查询查询出错: ' + err);
    emitter.emit('done', 'six', six);
  });

  Series.find({ "type": { "$in": [21, 19, 24] } }, function (err, seven) {
    if (err) console.log('seven查询查询出错: ' + err);
    emitter.emit('done', 'seven', seven);
  });


  var after = function (times, callback) {
    var count = 0, results = {};
    return function (key, value) {
      results[key] = value;
      count++;
      if (count == times)
      {
        res.render('home', {
          coverQueries: results['covers'],
          homeCatQueries: [results['one'], results['two'],
            results['three'], results['four'], results['five'], results['six'], results['seven']]
        });
      }
    };};

    var done = after(8);
    emitter.on("done", done);
};

Query#exec 返回的是Promise,可以用Promise.All

var expect = require('chai').expect
var User = require('../app/models/index.js').User
var assert = require('assert')


describe('Query Promise', function(){
  it('test', function(done){
    var a = User.find({uname: 'a'}).exec()
    var b = User.find({uname: 'd'}).exec()
    var c = User.find({uname: 'c'}).exec()
    var d = User.find({uname: 'e'}).exec()
    
    Promise.all([a,b,c,d]).then(function(results){
      console.log(results)
      done()
    })
    
  })
  
})

20233FD9-0627-4FAB-A520-93D1AE903C55.png

Promise.all或者aync.parallel可以的

回到顶部