通过从当前文档和下一个文档中减去一个值来查询以获取一个值

时间:2018-09-20 13:59:23

标签: mongodb aggregation-framework subtraction

我有一个如下的mongo db集合,

{
    "id": ObjectId("132456"),
    reading :[
        {
            "weight" : {
            "measurement" : 82.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-12T11:45:08.174Z")
},
{
    "id": ObjectId("132457"),
    reading :[
        {
            "weight" : {
            "measurement" : 80.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-12T10:45:08.174Z")
},
{
    "id": ObjectId("132458"),
    reading :[
        {
            "weight" : {
            "measurement" : 85.0,
            "unit" : "kg"
               }
        }
    ],
    "date" : ISODate("2018-09-11T09:45:08.174Z")
}

我需要一个mongo db查询,该查询将为我提供当前重量以及当前记录与下一条记录之间的重量差。 下面的示例输出,

{
    "id": ObjectId("132456"),
    "currentWeight": 75.0,
    "weightDifference": 2.0,
    "date" : ISODate("2018-09-12T11:45:08.174Z")
},
{
    "id": ObjectId("132457"),
    "currentWeight": 80.0,
    "weightDifference": -5.0,
    "date" : ISODate("2018-09-12T10:45:08.174Z")
}

我无法从下一个文档中获取权重以从当前文档中减去权重。 预先感谢您的帮助

我尝试解决上述问题,

db.measurementCollection.aggregate([
{
    $match : { "date" : { $gte : new ISODate("2018-09-01T00:00:00.000Z") , $lte : new ISODate("2018-09-12T23:59:59.000Z")  }  }
},
{ 
    $project : { "date" : 1 , 
    "currentWeight" : {$arrayElemAt: [ "$reading.weight.measurement", 0 ]}
},
{ $sort: {"date":-1} },
{ 
    $addFields : {
        "weigtDifference" : 
            {
                {
                    $limit: 2
                },
                {
                    $group: {
                      _id: null,
                      'count1': {$first: '$currentWeight'},
                      'count2': {$last: '$currentWeight'}
                    }
                },
              {
                $subtract: ['$count1', '$count2']
              }

            }
        }
}
])

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总,但我不建议您将其用于大型数据集。

db.collection.aggregate([
  { "$match": {
    "date" : {
      "$gte": new ISODate("2018-09-01T00:00:00.000Z"),
      "$lte": new ISODate("2018-09-12T23:59:59.000Z")
    }
  }},
  { "$unwind": "$reading" },
  { "$sort": { "date": -1 }},
  { "$group": { "_id": null, "data": { "$push": "$$ROOT" }}},
  { "$project": {
      "data": {
        "$filter": {
          "input": {
            "$map": {
              "input": { "$range": [0, { "$size": "$data" }] },
              "as": "tt",
              "in": {
                "$let": {
                  "vars": {
                    "first": { "$arrayElemAt": ["$data", "$$tt"] },
                    "second": { "$arrayElemAt": ["$data", { "$add": ["$$tt", 1] }] }
                  },
                  "in": {
                    "currentWeight": "$$first.reading.weight.measurement",
                    "weightDifference": { "$subtract": ["$$second.reading.weight.measurement", "$$first.reading.weight.measurement"] },
                    "_id": "$$first._id",
                    "date": "$$first.date"
                  }
                }
              }
            }
          },
          "cond": { "$ne": ["$$this.weightDifference", null] }
        }
      }
    }
  },
  { "$unwind": "$data" },
  { "$replaceRoot": { "newRoot": "$data" }}
])