带连接操作的MySQL查询转换为MongoDB

时间:2018-05-05 11:58:05

标签: mongodb

我需要将以下mysql查询转换为mongo。任何帮助将受到高度赞赏。

SELECT f.Datetime, c.Air Temperature(Celcius),f.Surface Temperature(Celcius) FROM firedata f JOIN climatedata c ON f.Date = c.Date where f.Confidence > 80 and f.Confidence < 100;

1 个答案:

答案 0 :(得分:0)

在MongoDB中,连接的数据要么嵌套在单个文档中(在这种情况下你不需要连接),要么在不同的文档中,最好的选择是聚合框架,特别是“查找”管道阶段

在不了解任何实现的更多细节的情况下,它将类似于:

fireDataCollection.aggregate([
    { 
       $match: {
          $and: [
            { "Confidence": { $gt: 80 } },
            { "Confidence": { $lt: 100 } }
          ]
       }           
    },
    {
       $lookup: {
          from: "climateDataCollection",
          localField: "Date",
          foreignField: "Date",
          as: "Climate"
       }
    },
    {
      $unwind: "$Climate"
    }
])

$ unwind步骤是可选的,它会将“气候”字段(在查找之后将是每个匹配日期的气候的集合)转换为多个文档,每个文档只包含一个“气候”的嵌套文档< / p>