【问题标题】:How can I make a union of mongoose queries in node.js?如何在 node.js 中合并 mongoose 查询?
【发布时间】:2012-08-23 12:17:48
【问题描述】:

这是我当前的代码:

Model.containing(req.user.location, function(err, models) {
  if (err) throw err;
  res.render('models/index', {title: 'Models', models: models});
});

我得到一个包含用户位置的模型列表。它工作得很好。现在我想获得所有以前的模型和一个额外的模型。我可以通过以下方式检索这个额外的模型:

Model.find().where('name').equals('desired name').exec(function(err, models) {
 //got my models!
});

我可以把它们结合起来做这样的事情吗?

Model.containing(req.user.location, function(err, models) {
  if (err) throw err;
  Model.find().where('name').equals('desired name').exec(function(err, newModels) {
    var allModels = models + newModels;
    res.render('models/index', {title: 'Models', models: allModels});
  });
});

谢谢!

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    差不多,但是由于modelsnewModels 是数组,您需要调用concat 来合并它们,而不是使用+

    var allModels = models.concat(newModels);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2021-01-23
      • 2012-12-09
      • 2017-10-19
      • 2016-01-24
      相关资源
      最近更新 更多