【问题标题】:Merge two mongo collections with query condition in pentaho在 pentaho 中合并两个带有查询条件的 mongo 集合
【发布时间】:2017-12-06 04:10:07
【问题描述】:

我在查询两个集合并将结果组合成另一个集合时遇到问题。

集合1:

{
    "ResourceCost" : 0.0032258065,
    "ResourceId" : "i-08c35f123eea43f30",
    "Unit" : "USD",
    "billdate" : ISODate("2017-12-01T16:30:00.000Z")
}

集合 2:

{
    "resource_id" : "i-08c35f123eea43f30",
    "Timestamp" : ISODate("2017-12-01T18:30:00.000Z"),
    "Avg" : 0.0,
    "total" : 0.0,
    "sample_cnt" : 1440.0,
    "max" : 0.0,
    "min" : 0.0
}

我需要使用集合 2 数据更新集合 1。所以最后我期待收集1个数据如下

同时检查字段和资源 ID。更新数据

集合 1:

{
   "ResourceCost" : 0.0032258065,
   "ResourceId" : "i-08c35f123eea43f30",
   "Unit" : "USD",
   "billdate" : ISODate("2017-12-01T16:30:00.000Z")
   "Avg" : 0.0,
   "total" : 0.0,
   "sample_cnt" : 1440.0,
   "max" : 0.0,
   "min" : 0.0
}

【问题讨论】:

    标签: mongodb pentaho pentaho-spoon pentaho-data-integration


    【解决方案1】:

    希望对你有帮助,

    db.col1.aggregate([
    {
        $lookup:{
                from:"col2",
                localField:"ResourceId",
                foreignField:"resource_id",
                as:"result"
            }
    },{
        $unwind:"$result"
    },{
        $project:{
                "ResourceCost":"$ResourceCost",
                "ResourceId":"$ResourceId",
                "Unit":"$Unit",
                "billdate":"$billdate",
                "_id":0,
                "avg":"$result.Avg",
                "total":"$result.total",
                "sample_cnt":"$result.sample_cnt",
                "max":"$result.max",
                "min":"$result.min"
            }
    }
    ])
    

    阅读 $loopup$unwind$project 以供参考。

    【讨论】:

      【解决方案2】:

      在 mongodb 3.4 中你可以使用这个

          db.col1.aggregate([
          {
              $lookup:{
                      from:"col2",
                      localField:"ResourceId",
                      foreignField:"resource_id",
                      as:"result"
                  }
          },   {
            $replaceRoot: { 
               newRoot: { 
                 $mergeObjects: [ { $arrayElemAt: [ "$result", 0 ] }, "$$ROOT" ] 
               } 
           }
         },{ 
           $project: { result: 0 } 
           }])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        • 1970-01-01
        • 2017-12-04
        • 1970-01-01
        相关资源
        最近更新 更多