【问题标题】:MongoDB select first result from documents meeting $inMongoDB 从满足 $in 的文档中选择第一个结果
【发布时间】:2020-04-20 06:37:47
【问题描述】:

我在循环中有这个查询:

const currentDatas = await Promise.all(nearestStations.map(async (ns: any) => {
        return await this.stationCurrentDataRepo.findOne({
            where: { stationId: parseInt(ns[0], 10) },
            order: { date: 'DESC' },
        });
}));

我想优化它以不进行数百次查询并在一次查询中获取数据。 我需要的是从 ids 数组($in ids 数组)中获取每个 stationId 的最新记录(按日期排序)。我需要所有找到的文档中的所有数据都符合我上面指定的要求。

【问题讨论】:

    标签: javascript node.js mongodb typescript typeorm


    【解决方案1】:

    在 MongoDB 中,这是通过聚合管道和 $group operator 完成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2018-10-14
      • 2015-07-09
      • 2011-12-11
      相关资源
      最近更新 更多