【问题标题】:Mongoosejs OR statement with ObjectIDs带有 ObjectID 的 Mongoosejs OR 语句
【发布时间】:2013-04-24 07:21:57
【问题描述】:

我正在查询我的团队架构并获取一组 id。我已经验证 teamIds 是一个包含正确 id 的数组。在我的OR 语句中,home_teamaway_team 都是 Team Schema 的 ObjectID。我感觉我的问题与 home_teamaway_team 都是 ObjectID 的事实有关。

Team.find({
    'conference.name': confName
}).select('_id').exec(function(err, results) {
    var teamIds = _.pluck(results, '_id');
    console.log(teamIds);

    finding = Game.find().or([
        {'home_team': { $in: teamIds }},
        {'away_team': { $in: teamIds }}
    ]);

    finding.exec(function(err, models) {
        // An error occurred
        if (err) return res.send(err, 500);

        // No models found
        if (!models) return res.send(404);

        console.log(models);
    });
});

此查询不起作用,我没有做错什么。

【问题讨论】:

  • 什么是finding,更具体地说是finding.or
  • @RoelvanUden 已更新,因此更有意义。

标签: javascript node.js express mongoose


【解决方案1】:

这个答案主要基于文档,而不是实验。似乎 mongoose or 函数使用了查询并将其包装到带有附加子句的 or 语句中。在您的情况下,您将搜索主队和客队中存在的“一切”或标识符。它应该在主队或客队,因此建议手动编写:

Game.find({$or: [
                 {'home_team': {$in: teamIds}},
                 {'away_team': {$in: teamIds}}
                 ]}, function(err, models) {
    // An error occurred
    if (err) return res.send(err, 500);

    // No models found
    if (!models) return res.send(404);

    console.log(models);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多